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

Commit 95e66d6

Browse files
matskomhevery
authored andcommitted
fix(NgForm): always return the first matching control when using map notation on a NgForm instance
1 parent 0cc1217 commit 95e66d6

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

lib/directive/ng_control.dart

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ abstract class NgControl implements NgDetachAware {
2626

2727
final Map<String, List<NgControl>> errors = new Map<String, List<NgControl>>();
2828
final List<NgControl> _controls = new List<NgControl>();
29-
final Map<String, NgControl> _controlByName = new Map<String, NgControl>();
29+
final Map<String, List<NgControl>> _controlByName = new Map<String, List<NgControl>>();
3030

3131
NgControl(Scope this._scope, dom.Element this._element, Injector injector,
3232
NgAnimate this._animate)
@@ -144,7 +144,7 @@ abstract class NgControl implements NgDetachAware {
144144
addControl(NgControl control) {
145145
_controls.add(control);
146146
if (control.name != null) {
147-
_controlByName[control.name] = control;
147+
_controlByName.putIfAbsent(control.name, () => new List<NgControl>()).add(control);
148148
}
149149
}
150150

@@ -157,8 +157,12 @@ abstract class NgControl implements NgDetachAware {
157157
*/
158158
removeControl(NgControl control) {
159159
_controls.remove(control);
160-
if (control.name != null) {
161-
_controlByName.remove(control.name);
160+
String key = control.name;
161+
if (key != null && _controlByName.containsKey(key)) {
162+
_controlByName[key].remove(control);
163+
if (_controlByName[key].isEmpty) {
164+
_controlByName.remove(key);
165+
}
162166
}
163167
}
164168

lib/directive/ng_form.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,11 @@ class NgForm extends NgControl {
5757
}
5858
}
5959

60-
NgControl operator[](name) => _controlByName[name];
60+
NgControl operator[](name) {
61+
if (_controlByName.containsKey(name)) {
62+
return _controlByName[name][0];
63+
}
64+
}
6165
}
6266

6367
class NgNullForm extends NgNullControl implements NgForm {

test/directive/ng_form_spec.dart

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,26 @@ void main() {
2020
expect(form.name).toEqual('myForm');
2121
}));
2222

23+
it('should return the first control with the given name when accessed using map notation',
24+
inject((Scope scope, TestBed _) {
25+
26+
var element = $('<form name="myForm">' +
27+
' <input type="text" name="model" ng-model="modelOne" probe="a" />' +
28+
' <input type="text" name="model" ng-model="modelTwo" probe="b" />' +
29+
'</form>');
30+
31+
_.compile(element);
32+
scope.apply();
33+
34+
NgForm form = _.rootScope.context['myForm'];
35+
NgModel one = _.rootScope.context['a'].directive(NgModel);
36+
NgModel two = _.rootScope.context['b'].directive(NgModel);
37+
38+
expect(one).not.toBe(two);
39+
expect(form['model']).toBe(one);
40+
expect(scope.eval("myForm['model']")).toBe(one);
41+
}));
42+
2343
describe('pristine / dirty', () {
2444
it('should be set to pristine by default', inject((Scope scope, TestBed _) {
2545
var element = $('<form name="myForm"></form>');

0 commit comments

Comments
 (0)