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

Commit

Permalink
feat(NgForm): provide access to non-uniquely named control instances …
Browse files Browse the repository at this point in the history
…via form.controls

Closes #642
  • Loading branch information
matsko authored and mhevery committed Mar 6, 2014
1 parent 95e66d6 commit 6099c03
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/directive/ng_form.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,18 @@ class NgForm extends NgControl {
}
}

get controls => _controlByName;

NgControl operator[](name) {
if (_controlByName.containsKey(name)) {
return _controlByName[name][0];
if (controls.containsKey(name)) {
return controls[name][0];
}
}
}

class NgNullForm extends NgNullControl implements NgForm {
NgNullForm() {}
operator[](name) {}

get controls => null;
}
24 changes: 24 additions & 0 deletions test/directive/ng_form_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,30 @@ void main() {
expect(scope.eval("myForm['model']")).toBe(one);
}));

it('should return the all the controls with the given name', inject((Scope scope, TestBed _) {
var element = $('<form name="myForm">' +
' <input type="text" name="model" ng-model="modelOne" probe="a" />' +
' <input type="text" name="model" ng-model="modelTwo" probe="b" />' +
'</form>');

_.compile(element);
scope.apply();

NgForm form = _.rootScope.context['myForm'];
NgModel one = _.rootScope.context['a'].directive(NgModel);
NgModel two = _.rootScope.context['b'].directive(NgModel);

expect(one).not.toBe(two);

var controls = form.controls['model'];
expect(controls[0]).toBe(one);
expect(controls[1]).toBe(two);

expect(scope.eval("myForm.controls['model'][0]")).toBe(one);
expect(scope.eval("myForm.controls['model'][1]")).toBe(two);
}));


describe('pristine / dirty', () {
it('should be set to pristine by default', inject((Scope scope, TestBed _) {
var element = $('<form name="myForm"></form>');
Expand Down

0 comments on commit 6099c03

Please sign in to comment.