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

Commit 4c9b804

Browse files
alorenzenmhevery
authored andcommitted
fix(ngControl): unregister control from parent on detach
Un-register a control with its parent on detach, and also move register call to attach(). Right now, when a form element(such as an input) is removed from the dom, the parent control does not remove it from the list of controls. Closes #684
1 parent 5a14408 commit 4c9b804

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

lib/directive/ng_control.dart

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
part of angular.directive;
22

3-
abstract class NgControl implements NgDetachAware {
3+
abstract class NgControl implements NgAttachAware, NgDetachAware {
44
static const NG_VALID_CLASS = "ng-valid";
55
static const NG_INVALID_CLASS = "ng-invalid";
66
static const NG_PRISTINE_CLASS = "ng-pristine";
@@ -38,10 +38,17 @@ abstract class NgControl implements NgDetachAware {
3838
_scope.on('submitNgControl').listen((e) => _onSubmit(e.data));
3939
}
4040

41+
@override
42+
attach() {
43+
_parentControl.addControl(this);
44+
}
45+
46+
@override
4147
detach() {
4248
for (int i = _controls.length - 1; i >= 0; --i) {
4349
removeControl(_controls[i]);
4450
}
51+
_parentControl.removeControl(this);
4552
}
4653

4754
reset() {
@@ -70,7 +77,6 @@ abstract class NgControl implements NgDetachAware {
7077
get name => _name;
7178
set name(value) {
7279
_name = value;
73-
_parentControl.addControl(this);
7480
}
7581

7682
get element => _element;
@@ -246,6 +252,7 @@ class NgNullControl implements NgControl {
246252
set untouched(value) {}
247253

248254
reset() => null;
255+
attach() => null;
249256
detach() => null;
250257
bool hasError(String key) => false;
251258

test/directive/ng_form_spec.dart

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,23 @@ void main() {
321321
expect(form['two']).toBeNull();
322322
expect(form['three']).toBeNull();
323323
}));
324+
325+
it('should remove from parent when child is removed', inject((Scope scope, TestBed _) {
326+
var element = $('<form name="myForm">' +
327+
' <input type="text" name="mega_name" ng-if="mega_visible" ng-model="value"/>' +
328+
'</form>');
329+
_.compile(element);
330+
331+
scope.context['mega_visible'] = true;
332+
scope.apply();
333+
334+
var form = scope.context['myForm'];
335+
expect(form['mega_name']).toBeDefined();
336+
337+
scope.context['mega_visible'] = false;
338+
scope.apply();
339+
expect(form['mega_name']).toBeNull();
340+
}));
324341
});
325342

326343
describe('onSubmit', () {

0 commit comments

Comments
 (0)