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

ngStyle fails when use !important #5379

Closed
GabrielDelepine opened this issue Dec 12, 2013 · 4 comments
Closed

ngStyle fails when use !important #5379

GabrielDelepine opened this issue Dec 12, 2013 · 4 comments

Comments

@GabrielDelepine
Copy link

Tested with angular 1.2.4

Example : http://jsfiddle.net/yappli/X8era/4/

@caitp
Copy link
Contributor

caitp commented Dec 12, 2013

You're not able to use the !important directive in the DOM style property in either Chrome nor FF (probably others too). I'm not sure this would work in jQuery either. edit I guess I was writing something here and didn't finish! So, the style attribute gets parsed as CSS, but the HTMLElement.style property doesn't. Unfortunately you're not able to set a property's priority here (in Gecko/Blink/Webkit, at least).

So, there are some work arounds here:

<ANY data-ng-attr-style="{{ blue && 'background-color: blue!important' || '' }}">
</ANY>

This would be your best way to go in terms of browser-support, because the CSS property priority will get parsed correctly here.

Alternatively, you can also do the following in javascript:

$scope.$watch(conditionalStyle);

function conditionalStyle() {
  if ($scope.blue) {
    $element[0].style.setProprety('background-color', 'blue', 'important');
  } else {
    $element[0].style.backgroundColor = '';
  }
}

Where $element is a jquery/jqLite object referencing an HTMLElement.

caveats: Not supported in IE < 9.

So, workaround #1 is probably your best bet for this behaviour where you depend on property priority...


original answer here

In any case, inline styles tend to take precedence over those found in stylesheets, except for those with '!important'

If you really need this, you can use ng-attr-style to do it, at least in chrome/ff28. http://plnkr.co/edit/drS4oqb09gOIJeXjCfJo?p=preview

I suppose jqLite could be hacked to use CSSStyleDeclaration.setProperty() with the 'important' priority if it sees !important in the string, but I'm just not sure if it's worth it.

@GabrielDelepine
Copy link
Author

Thanks for your answer. I will do tests with style and !important. I will report-it here soon.

@caitp
Copy link
Contributor

caitp commented Dec 12, 2013

If it helps, according to http://help.dottoro.com/ljdpsdnb.php, IE8 and less don't provide the CSSStyleDeclaration.setProperty(), however in the future it might be a good performance improvement over the existing implementation of ngStyle / jqLite.css() --- less string manipulation needed, and the bonus of supporting style priority

@GabrielDelepine
Copy link
Author

Thanks for your comprehensive answer @caitp. It helps a lot.

I will close this issue. Maybe I will open-it back for Angular 2.0, when IE8 will be no longuer supported.

thegitfather pushed a commit to thegitfather/ng-file-upload that referenced this issue May 3, 2016
jqLite .css() function has problems with `!important`. See e.g.:
angular/angular.js#5379
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants