Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 586e2ac

Browse files
jonathantyatesgkalpak
authored andcommitted
fix($compile): clean up @-binding observers when re-assigning bindings
Fixes #15268 Closes #15298
1 parent 41034bb commit 586e2ac

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

src/ng/compile.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3434,7 +3434,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
34343434
if (!optional && !hasOwnProperty.call(attrs, attrName)) {
34353435
destination[scopeName] = attrs[attrName] = undefined;
34363436
}
3437-
attrs.$observe(attrName, function(value) {
3437+
removeWatch = attrs.$observe(attrName, function(value) {
34383438
if (isString(value) || isBoolean(value)) {
34393439
var oldValue = destination[scopeName];
34403440
recordChanges(scopeName, value, oldValue);
@@ -3453,6 +3453,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
34533453
destination[scopeName] = lastValue;
34543454
}
34553455
initialChanges[scopeName] = new SimpleChange(_UNINITIALIZED_VALUE, destination[scopeName]);
3456+
removeWatchCollection.push(removeWatch);
34563457
break;
34573458

34583459
case '=':

test/ng/compileSpec.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4378,6 +4378,37 @@ describe('$compile', function() {
43784378
});
43794379
});
43804380

4381+
it('should clean up `@`-binding observers when re-assigning bindings', function() {
4382+
var constructorSpy = jasmine.createSpy('constructor');
4383+
var prototypeSpy = jasmine.createSpy('prototype');
4384+
4385+
function TestController() {
4386+
return {$onChanges: constructorSpy};
4387+
}
4388+
TestController.prototype.$onChanges = prototypeSpy;
4389+
4390+
module(function($compileProvider) {
4391+
$compileProvider.component('test', {
4392+
bindings: {attr: '@'},
4393+
controller: TestController
4394+
});
4395+
});
4396+
4397+
inject(function($compile, $rootScope) {
4398+
var template = '<test attr="{{a}}"></test>';
4399+
$rootScope.a = 'foo';
4400+
4401+
element = $compile(template)($rootScope);
4402+
$rootScope.$digest();
4403+
expect(constructorSpy).toHaveBeenCalled();
4404+
expect(prototypeSpy).not.toHaveBeenCalled();
4405+
4406+
constructorSpy.calls.reset();
4407+
$rootScope.$apply('a = "bar"');
4408+
expect(constructorSpy).toHaveBeenCalled();
4409+
expect(prototypeSpy).not.toHaveBeenCalled();
4410+
});
4411+
});
43814412

43824413
it('should not call `$onChanges` twice even when the initial value is `NaN`', function() {
43834414
var onChangesSpy = jasmine.createSpy('$onChanges');

0 commit comments

Comments
 (0)