-
Notifications
You must be signed in to change notification settings - Fork 27.4k
ngStyle not removing values #16860
ngStyle not removing values #16860
Comments
Seems to be introduced in 1.7.6 |
Yes, this was indeed introduced in d6098ee. The reason it happens is that browsers seem to ignore invalid values for styles. Previously, But, when the property value is invalid (in this case, when Not sure how to address this. It is not possible to detect whether a value is valid or not (OK, it might be possible, but it would be prohibitively costly), so we need to either:
|
Correct, the 'false' value is the culprit. BTW, how many DOM style properties support boolean values instead of strings? Most probably in the majority of the cases the 'false' value can safely remove the property. Thanks, L |
Since d6098ee, old styles were not removed if `newStyles` specified an invalid value for the style (e.g. `false`). The assumption was that the new style would overwrite the old style value, but using an invalid value made browsers ignore the new value and thus keep the old style. This would typically happen when guarding a style with a boolean flag; e.g.: `ng-style="{backgroundColor: isError && 'red'}"` This commit essentially revers commit d6098ee, whose main purpose was to work around jquery/jquery#4185. The jQuery issue has been fixed in 3.4.0, so that should not be a problem any more. Fixes angular#16860
Since d6098ee, old styles were not removed if `newStyles` specified an invalid value for the style (e.g. `false`). The assumption was that the new style would overwrite the old style value, but using an invalid value made browsers ignore the new value and thus keep the old style. This would typically happen when guarding a style with a boolean flag; e.g.: `ng-style="{backgroundColor: isError && 'red'}"` This commit essentially revers commit d6098ee, whose main purpose was to work around jquery/jquery#4185. The jQuery issue has been fixed in 3.4.0, so that should not be a problem any more. Fixes #16860 Closes #16868
I'm submitting a ...
Current behavior:
In AngularJS 1.6 (and even in 1.7.0) the ngStyle directive was able to add/remove single properties when the expression for the single property was evaluated as false.
For example:
ng-style="{'float': isRight && 'right' }"
was dynamically adding/removing the 'float: right' style when the isRight scope expression changed.
In Angular 1.7.8 this only happens when isRight switches from false to true. Switching back to false will leave the 'float: right' style set.
As a workaround, this expression instead works in both 1.6.x and 1.7.8:
ng-style="isRight && {'float': 'right' }"
Expected / new behavior:
Expressions like
ng-style="{'float': isRight && 'right' }"
should be still supported for backward compatibility. Or at least the change (if intentional) should be documented in the changelog documentation.
Regards
Minimal reproduction of the problem with instructions:
Here a plunkr to replicate the issue:
https://plnkr.co/edit/FsWbrVvfAYvuJCoGszi2?p=preview
AngularJS version: 1.7.8
Browser: [all]
The text was updated successfully, but these errors were encountered: