This repository was archived by the owner on Apr 12, 2024. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +33
-1
lines changed Expand file tree Collapse file tree 2 files changed +33
-1
lines changed Original file line number Diff line number Diff line change @@ -3434,7 +3434,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
3434
3434
if ( ! optional && ! hasOwnProperty . call ( attrs , attrName ) ) {
3435
3435
destination [ scopeName ] = attrs [ attrName ] = undefined ;
3436
3436
}
3437
- attrs . $observe ( attrName , function ( value ) {
3437
+ removeWatch = attrs . $observe ( attrName , function ( value ) {
3438
3438
if ( isString ( value ) || isBoolean ( value ) ) {
3439
3439
var oldValue = destination [ scopeName ] ;
3440
3440
recordChanges ( scopeName , value , oldValue ) ;
@@ -3453,6 +3453,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
3453
3453
destination [ scopeName ] = lastValue ;
3454
3454
}
3455
3455
initialChanges [ scopeName ] = new SimpleChange ( _UNINITIALIZED_VALUE , destination [ scopeName ] ) ;
3456
+ removeWatchCollection . push ( removeWatch ) ;
3456
3457
break ;
3457
3458
3458
3459
case '=' :
Original file line number Diff line number Diff line change @@ -4378,6 +4378,37 @@ describe('$compile', function() {
4378
4378
} ) ;
4379
4379
} ) ;
4380
4380
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
+ } ) ;
4381
4412
4382
4413
it ( 'should not call `$onChanges` twice even when the initial value is `NaN`' , function ( ) {
4383
4414
var onChangesSpy = jasmine . createSpy ( '$onChanges' ) ;
You can’t perform that action at this time.
0 commit comments