diff --git a/src/ng/compile.js b/src/ng/compile.js index 77a13ab22a09..95526801bb2f 100644 --- a/src/ng/compile.js +++ b/src/ng/compile.js @@ -769,7 +769,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) { * @param {function(interpolatedValue)} fn Function that will be called whenever the interpolated value of the attribute changes. * See the {@link guide/directive#Attributes Directives} guide for more info. - * @returns {function()} the `fn` parameter. + * @returns {function()} Returns a deregistration function for this observer. */ $observe: function(key, fn) { var attrs = this, @@ -783,7 +783,10 @@ function $CompileProvider($provide, $$sanitizeUriProvider) { fn(attrs[key]); } }); - return fn; + + return function() { + arrayRemove(listeners, fn); + }; } }; diff --git a/test/ng/compileSpec.js b/test/ng/compileSpec.js index c37461fcc007..44b14e1e6d86 100755 --- a/test/ng/compileSpec.js +++ b/test/ng/compileSpec.js @@ -1749,15 +1749,14 @@ describe('$compile', function() { describe('interpolation', function() { - var observeSpy, directiveAttrs; + var observeSpy, directiveAttrs, deregisterObserver; beforeEach(module(function() { directive('observer', function() { return function(scope, elm, attr) { directiveAttrs = attr; observeSpy = jasmine.createSpy('$observe attr'); - - expect(attr.$observe('someAttr', observeSpy)).toBe(observeSpy); + deregisterObserver = attr.$observe('someAttr', observeSpy); }; }); directive('replaceSomeAttr', valueFn({ @@ -1855,6 +1854,18 @@ describe('$compile', function() { })); + it('should return a deregistration function while observing an attribute', inject(function($rootScope, $compile) { + $compile('
')($rootScope); + + $rootScope.$apply('value = "first-value"'); + expect(observeSpy).toHaveBeenCalledWith('first-value'); + + deregisterObserver(); + $rootScope.$apply('value = "new-value"'); + expect(observeSpy).not.toHaveBeenCalledWith('new-value'); + })); + + it('should set interpolated attrs to initial interpolation value', inject(function($rootScope, $compile) { $rootScope.whatever = 'test value'; $compile('')($rootScope);