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

Commit 15bbd3e

Browse files
PatrickJSjbedard
authored andcommitted
perf(ngStyleDirective): use $watchCollection
Since we are simply watching a flat object collection it is more performant to use $watchCollection than a deepWatch... BREAKING CHANGE: Previously the use of deep watch by ng-style would trigger styles to be re-applied when nested state changed. Now only changes to direct properties of the watched object will trigger changes. Closes #15947
1 parent 87a586e commit 15bbd3e

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/ng/directive/ngStyle.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@
5252
</example>
5353
*/
5454
var ngStyleDirective = ngDirective(function(scope, element, attr) {
55-
scope.$watch(attr.ngStyle, function ngStyleWatchAction(newStyles, oldStyles) {
55+
scope.$watchCollection(attr.ngStyle, function ngStyleWatchAction(newStyles, oldStyles) {
5656
if (oldStyles && (newStyles !== oldStyles)) {
5757
forEach(oldStyles, function(val, style) { element.css(style, '');});
5858
}
5959
if (newStyles) element.css(newStyles);
60-
}, true);
60+
});
6161
});

test/ng/directive/ngStyleSpec.js

+15
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,21 @@ describe('ngStyle', function() {
2323
}));
2424

2525

26+
it('should not deep watch objects', inject(function($rootScope, $compile) {
27+
element = $compile('<div ng-style="{height: heightObj}"></div>')($rootScope);
28+
$rootScope.$digest();
29+
expect(parseInt(element.css('height') + 0, 10)).toEqual(0); // height could be '' or '0px'
30+
$rootScope.heightObj = {toString: function() { return '40px'; }};
31+
$rootScope.$digest();
32+
expect(element.css('height')).toBe('40px');
33+
34+
element.css('height', '10px');
35+
$rootScope.heightObj.otherProp = 123;
36+
$rootScope.$digest();
37+
expect(element.css('height')).toBe('10px');
38+
}));
39+
40+
2641
it('should support lazy one-time binding for object literals', inject(function($rootScope, $compile) {
2742
element = $compile('<div ng-style="::{height: heightStr}"></div>')($rootScope);
2843
$rootScope.$digest();

0 commit comments

Comments
 (0)