File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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 '=' :
Original file line number Diff line number Diff 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' ) ;
You can’t perform that action at this time.
0 commit comments