Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manipulating inline style with angular does not work in IE

I wanted to set the position of a div based on the return value of a function in an angular controller

The following works fine in FireFox and in chrome but in Internet explorer {{position($index)}}% is interpreted as a literal string value and therefore has no effect

<div ng-repeat="item in items" style="left:{{position($index)}}%"></div> 

Here is an example of the issue:

var app = angular.module('app', []);  app.controller('controller', function($scope) {      $scope.items=[1,2,3,4,5,6,7,8,9,10];      $scope.position=function(i){         var percent =[5,10,15,20,25,30,35,40,45,50,55,60,65,70];         return percent[i+1];     } }); 

And here is a Fiddle to demonstrate

Does anyone have suggestions on how to rectify?

like image 232
andrew Avatar asked Sep 04 '14 00:09

andrew


People also ask

Why you shouldn't use inline styles?

Inline styles, while they have a purpose, generally are not the best way to maintain your website. They go against every one of the best practices: Inline styles don't separate content from design: Inline styles are exactly the same as embedded font and other clunky design tags that modern developers rail against.

What are the disadvantages of inline styles?

Disadvantages of Inline CSS:Adding CSS rules to every HTML element is time-consuming and makes your HTML structure messy. Styling multiple elements can affect your page's size and download time.

How do you style an inline?

Inline - by using the style attribute inside HTML elements. Internal - by using a <style> element in the <head> section. External - by using a <link> element to link to an external CSS file.

Is inline styling slower?

Yes, inline styles are technically faster than an external stylesheet because you are making one less request on top of the page but using an external stylesheet is much preferred for code maintainability.


2 Answers

You must use ng-style instead of style, otherwise some browsers like IE will remove invalid style attribute values (presence of {{}} etc makes it invalid) before even angular has a chance to render it. When you use ng-style angular will calculate the expression and add the inline style attributes to it.

<div ng-repeat="item in items" ng-style="{left: position($index) + '%'}"></div> 

Since you are anyways calculating the position you could as well add % from the position and send it. Also remember that calling a function in ng-repeat will invoke the function every digest cycle, so you may want to be careful not to do too much intensive operations inside the method.

 <div ng-repeat="item in items" ng-style="{left: position($index)}">{{item}}</div> 

and return

  return percent[i+1] + "%"; 

Demo

like image 59
PSL Avatar answered Oct 14 '22 00:10

PSL


If you want to use angular binding expression {{}} just like normal style attribute like style="width:{{someScopeVar}}", use ng-attr-style and it will work perfectly IE (and obviously other smarter ones) :)

check my jsFiddle ... Checked with Angular JS 1.4.8

here I have shown the usage of style, ng-style and ng-attr-style

THE HTML

<div ng-app="app">     <div ng-controller="controller">          <div style="background:{{bgColor}}">             This will NOT get colored in IE         </div>          <div ng-attr-style="background:{{bgColor}}">             But this WILL get colored in IE         </div>          <div ng-style="styleObject">             And so is this... as this uses a json object and gives that to ng-style         </div>      </div> </div> 

THE JS

var app = angular.module('app', []);  app.controller('controller', function($scope) {      $scope.bgColor = "yellow";             $scope.styleObject = {"background": "yellow"}; }); 
like image 35
Suman Barick Avatar answered Oct 14 '22 00:10

Suman Barick