jcubic jcubic - 4 months ago 26
CSS Question

Angular expression inside style attribute don't work in IE

I have angular 1.0.6 (I know it's old) and I have style attribute with expressions:

<li style="background-color: {{item.color}}">
<span style="color: {{item.color | contrastColor}}">{{item.label}}</span>
</li>


It work fine but not for IE (The app need to work for >IE10). When I open Developer tool the style attribute is not present. I've try to create custom style directive (because I tought that IE remove invalid attribute before Angular can read it) but with this simple code, I've got an error
TypeError: Cannot read property 'replace' of undefined
from jquery (tested on google chrome) because in my case item.color can be null

.directive("logStyle", function() {
// logStyle directive
return {
restrict: 'A',
link: function(scope, element, attrs) {
element.css(scope.$eval(attrs.logStyle));
}
};
});


How can I make it work. I know that there is ngStyle but it's not quite what I need.

Answer

Ok, try this but I'm not sure if I fully understand what your trying to do

<div ng-controller="appCtrl">
<ul>
    <li ng-repeat="item in items" ng-style="{'background-color': item.color}">
        <span ng-style="{ 'color': (item.color | contrastColor) }">{{ item.label }}</span>
    </li>
</ul>
</div>

Edited the html, couldn't test this on IE last night so had to wait until today. IE seemingly doesn't like the style attribute with {{ }} binding inside so it deletes it from the DOM. I found this issue https://github.com/angular/angular.js/issues/2186 and there is a plunkr with the fix given.