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

fix(ngStyle): skip setting empty value when new style has the property #16709

Merged
merged 7 commits into from
Oct 4, 2018
Merged
Show file tree
Hide file tree
Changes from 4 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
6 changes: 5 additions & 1 deletion src/ng/directive/ngStyle.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@
var ngStyleDirective = ngDirective(function(scope, element, attr) {
scope.$watchCollection(attr.ngStyle, function ngStyleWatchAction(newStyles, oldStyles) {
if (oldStyles && (newStyles !== oldStyles)) {
forEach(oldStyles, function(val, style) { element.css(style, '');});
forEach(oldStyles, function(val, style) {
if (!(newStyles && newStyles[style])) {
element.css(style, '');
Copy link
Collaborator

@jbedard jbedard Oct 3, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to do newStyles[style] = '' instead (maybe also needing a newStyles null check + assign). This way we will only make a single element.css(newStyles) call at the end instead of multiple .css calls.

Also should this be an undefined/null check instead of just falsey check? What if newStyles[style] is 0?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For sure. Fixed in 95d2458.

}
});
}
if (newStyles) element.css(newStyles);
});
Expand Down
11 changes: 11 additions & 0 deletions test/ng/directive/ngStyleSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,5 +120,16 @@ describe('ngStyle', function() {
expect(element.css(preCompStyle)).not.toBe('88px');
expect(element.css(postCompStyle)).not.toBe('99px');
});

it('should clear style when the value is falsy', function() {
scope.styleObj = {'height': '99px', 'width': '88px'};
scope.$apply();
expect(element.css(preCompStyle)).toBe('88px');
expect(element.css(postCompStyle)).toBe('99px');
scope.styleObj = {'height': undefined, 'width': null};
scope.$apply();
expect(element.css(preCompStyle)).not.toBe('88px');
expect(element.css(postCompStyle)).not.toBe('99px');
});
});
});