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

Commit 299b220

Browse files
caiotoonIgorMinar
authored andcommitted
feat($compile): add support for $observer deregistration
In order to make the behavior compatible with $rootScope.$watch and $rootScope.$on methods, and make it possible to deregister an attribute observer, Attributes.$observe method now returns a deregistration function instead of the observer itself. BREAKING CHANGE: calling attr.$observe no longer returns the observer function, but a deregistration function instead. To migrate the code follow the example below: Before: ``` directive('directiveName', function() { return { link: function(scope, elm, attr) { var observer = attr.$observe('someAttr', function(value) { console.log(value); }); } }; }); ``` After: ``` directive('directiveName', function() { return { link: function(scope, elm, attr) { var observer = function(value) { console.log(value); }; attr.$observe('someAttr', observer); } }; }); ``` Closes #5609
1 parent 78057a9 commit 299b220

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

src/ng/compile.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -775,7 +775,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
775775
* @param {function(interpolatedValue)} fn Function that will be called whenever
776776
the interpolated value of the attribute changes.
777777
* See the {@link guide/directive#Attributes Directives} guide for more info.
778-
* @returns {function()} the `fn` parameter.
778+
* @returns {function()} Returns a deregistration function for this observer.
779779
*/
780780
$observe: function(key, fn) {
781781
var attrs = this,
@@ -789,7 +789,10 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
789789
fn(attrs[key]);
790790
}
791791
});
792-
return fn;
792+
793+
return function() {
794+
arrayRemove(listeners, fn);
795+
};
793796
}
794797
};
795798

test/ng/compileSpec.js

+14-3
Original file line numberDiff line numberDiff line change
@@ -1914,15 +1914,14 @@ describe('$compile', function() {
19141914

19151915

19161916
describe('interpolation', function() {
1917-
var observeSpy, directiveAttrs;
1917+
var observeSpy, directiveAttrs, deregisterObserver;
19181918

19191919
beforeEach(module(function() {
19201920
directive('observer', function() {
19211921
return function(scope, elm, attr) {
19221922
directiveAttrs = attr;
19231923
observeSpy = jasmine.createSpy('$observe attr');
1924-
1925-
expect(attr.$observe('someAttr', observeSpy)).toBe(observeSpy);
1924+
deregisterObserver = attr.$observe('someAttr', observeSpy);
19261925
};
19271926
});
19281927
directive('replaceSomeAttr', valueFn({
@@ -2020,6 +2019,18 @@ describe('$compile', function() {
20202019
}));
20212020

20222021

2022+
it('should return a deregistration function while observing an attribute', inject(function($rootScope, $compile) {
2023+
$compile('<div some-attr="{{value}}" observer></div>')($rootScope);
2024+
2025+
$rootScope.$apply('value = "first-value"');
2026+
expect(observeSpy).toHaveBeenCalledWith('first-value');
2027+
2028+
deregisterObserver();
2029+
$rootScope.$apply('value = "new-value"');
2030+
expect(observeSpy).not.toHaveBeenCalledWith('new-value');
2031+
}));
2032+
2033+
20232034
it('should set interpolated attrs to initial interpolation value', inject(function($rootScope, $compile) {
20242035
$rootScope.whatever = 'test value';
20252036
$compile('<div some-attr="{{whatever}}" observer></div>')($rootScope);

0 commit comments

Comments
 (0)