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

Commit

Permalink
fix(ngControl): unregister control from parent on detach
Browse files Browse the repository at this point in the history
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
  • Loading branch information
alorenzen authored and mhevery committed Mar 6, 2014
1 parent 5a14408 commit 4c9b804
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
11 changes: 9 additions & 2 deletions lib/directive/ng_control.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
part of angular.directive;

abstract class NgControl implements NgDetachAware {
abstract class NgControl implements NgAttachAware, NgDetachAware {
static const NG_VALID_CLASS = "ng-valid";
static const NG_INVALID_CLASS = "ng-invalid";
static const NG_PRISTINE_CLASS = "ng-pristine";
Expand Down Expand Up @@ -38,10 +38,17 @@ abstract class NgControl implements NgDetachAware {
_scope.on('submitNgControl').listen((e) => _onSubmit(e.data));
}

@override
attach() {
_parentControl.addControl(this);
}

@override
detach() {
for (int i = _controls.length - 1; i >= 0; --i) {
removeControl(_controls[i]);
}
_parentControl.removeControl(this);
}

reset() {
Expand Down Expand Up @@ -70,7 +77,6 @@ abstract class NgControl implements NgDetachAware {
get name => _name;
set name(value) {
_name = value;
_parentControl.addControl(this);
}

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

reset() => null;
attach() => null;
detach() => null;
bool hasError(String key) => false;

Expand Down
17 changes: 17 additions & 0 deletions test/directive/ng_form_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,23 @@ void main() {
expect(form['two']).toBeNull();
expect(form['three']).toBeNull();
}));

it('should remove from parent when child is removed', inject((Scope scope, TestBed _) {
var element = $('<form name="myForm">' +
' <input type="text" name="mega_name" ng-if="mega_visible" ng-model="value"/>' +
'</form>');
_.compile(element);

scope.context['mega_visible'] = true;
scope.apply();

var form = scope.context['myForm'];
expect(form['mega_name']).toBeDefined();

scope.context['mega_visible'] = false;
scope.apply();
expect(form['mega_name']).toBeNull();
}));
});

describe('onSubmit', () {
Expand Down

0 comments on commit 4c9b804

Please sign in to comment.