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

perf(ngStyleDirective): use $watchCollection #15947

Merged
merged 2 commits into from
Jul 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
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);
});
});
27 changes: 27 additions & 0 deletions test/ng/directive/ngStyleSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,33 @@ 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 binding for object literals', inject(function($rootScope, $compile) {
element = $compile('<div ng-style="{height: heightStr}"></div>')($rootScope);
$rootScope.$digest();
expect(parseInt(element.css('height') + 0, 10)).toEqual(0); // height could be '' or '0px'
$rootScope.$apply('heightStr = "40px"');
expect(element.css('height')).toBe('40px');

$rootScope.$apply('heightStr = "100px"');
expect(element.css('height')).toBe('100px');
}));


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