Skip to content

Commit e8bf846

Browse files
committed
fix(asDirectiveSpec): nullify the assigned controller when the element is removed
1 parent 27833b9 commit e8bf846

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

src/ng/directive/as.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ var componentAsDirective = ['$parse', function($parse) {
5858
// gets the controller of the current element (see jqLiteController for details)
5959
var controller = iElement.data('$' + controllerName + 'Controller');
6060
setter(scope, controller);
61+
62+
// when the element is removed, remove it from the scope assignment (nullify it)
63+
iElement.on('$destroy', function() {
64+
setter(scope, null);
65+
});
6166
};
6267
}
6368
};

test/ng/directive/asSpec.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,16 @@ describe('as', function() {
5454
expect($rootScope.$ctrl.myComponent2).toBe(myComponentController);
5555
});
5656

57+
it('should nullify the variable once the component is destroyed', function() {
58+
$rootScope.$ctrl = {};
59+
$compile('<div ng-if="!nullified"><my-component as="$ctrl.myComponent"></my-component></div>')($rootScope);
60+
$rootScope.$apply('nullified = false');
61+
expect($rootScope.$ctrl.myComponent).toBe(myComponentController);
62+
63+
$rootScope.$apply('nullified = true');
64+
expect($rootScope.$ctrl.myComponent).toBe(null);
65+
});
66+
5767
});
5868

5969
it('should be compatible with directives on entities with controller', function() {

0 commit comments

Comments
 (0)