Skip to content
This repository was archived by the owner on Feb 22, 2018. It is now read-only.

Commit 6099c03

Browse files
matskomhevery
authored andcommitted
feat(NgForm): provide access to non-uniquely named control instances via form.controls
Closes #642
1 parent 95e66d6 commit 6099c03

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

lib/directive/ng_form.dart

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,18 @@ class NgForm extends NgControl {
5757
}
5858
}
5959

60+
get controls => _controlByName;
61+
6062
NgControl operator[](name) {
61-
if (_controlByName.containsKey(name)) {
62-
return _controlByName[name][0];
63+
if (controls.containsKey(name)) {
64+
return controls[name][0];
6365
}
6466
}
6567
}
6668

6769
class NgNullForm extends NgNullControl implements NgForm {
6870
NgNullForm() {}
6971
operator[](name) {}
72+
73+
get controls => null;
7074
}

test/directive/ng_form_spec.dart

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,30 @@ void main() {
4040
expect(scope.eval("myForm['model']")).toBe(one);
4141
}));
4242

43+
it('should return the all the controls with the given name', inject((Scope scope, TestBed _) {
44+
var element = $('<form name="myForm">' +
45+
' <input type="text" name="model" ng-model="modelOne" probe="a" />' +
46+
' <input type="text" name="model" ng-model="modelTwo" probe="b" />' +
47+
'</form>');
48+
49+
_.compile(element);
50+
scope.apply();
51+
52+
NgForm form = _.rootScope.context['myForm'];
53+
NgModel one = _.rootScope.context['a'].directive(NgModel);
54+
NgModel two = _.rootScope.context['b'].directive(NgModel);
55+
56+
expect(one).not.toBe(two);
57+
58+
var controls = form.controls['model'];
59+
expect(controls[0]).toBe(one);
60+
expect(controls[1]).toBe(two);
61+
62+
expect(scope.eval("myForm.controls['model'][0]")).toBe(one);
63+
expect(scope.eval("myForm.controls['model'][1]")).toBe(two);
64+
}));
65+
66+
4367
describe('pristine / dirty', () {
4468
it('should be set to pristine by default', inject((Scope scope, TestBed _) {
4569
var element = $('<form name="myForm"></form>');

0 commit comments

Comments
 (0)