Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
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
  • Loading branch information
PatrickJS authored and jbedard committed Jul 27, 2017
commit a7cc066c789a55fe89fba36122a51080991fecb2
4 changes: 2 additions & 2 deletions src/ng/directive/ngStyle.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@
</example>
*/
var ngStyleDirective = ngDirective(function(scope, element, attr) {
scope.$watch(attr.ngStyle, function ngStyleWatchAction(newStyles, oldStyles) {
scope.$watchCollection(attr.ngStyle, function ngStyleWatchAction(newStyles, oldStyles) {
if (oldStyles && (newStyles !== oldStyles)) {
forEach(oldStyles, function(val, style) { element.css(style, '');});
}
if (newStyles) element.css(newStyles);
}, true);
});
});
15 changes: 15 additions & 0 deletions test/ng/directive/ngStyleSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,21 @@ describe('ngStyle', function() {
}));


it('should not deep watch objects', inject(function($rootScope, $compile) {
element = $compile('<div ng-style="{height: heightObj}"></div>')($rootScope);
$rootScope.$digest();
expect(parseInt(element.css('height') + 0, 10)).toEqual(0); // height could be '' or '0px'
$rootScope.heightObj = {toString: function() { return '40px'; }};
$rootScope.$digest();
expect(element.css('height')).toBe('40px');

element.css('height', '10px');
$rootScope.heightObj.otherProp = 123;
$rootScope.$digest();
expect(element.css('height')).toBe('10px');
}));


it('should support lazy one-time binding for object literals', inject(function($rootScope, $compile) {
element = $compile('<div ng-style="::{height: heightStr}"></div>')($rootScope);
$rootScope.$digest();
Expand Down