Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular Bug IE10 variable in style attribute

Tags:

angularjs

If I do something like this:

<div style="margin-left: 50px;">TEST1</div>
<div style="margin-left: {{50}}px;">TEST2</div>

http://jsfiddle.net/XeVHL/1/

The margin-left of the value test2 will not be applied, and this only in IE, any suggestion for a work around this?

enter image description here

like image 835
Jerome Ansia Avatar asked Sep 25 '13 20:09

Jerome Ansia


People also ask

How do I set a dynamic style in angular?

Use style binding to set styles dynamically. Binding to a single style link To create a single style binding, use the prefix style followed by a dot and the name of the CSS style property—for example, [style.width]="width". Angular sets the property to the value of the bound expression, which is usually a string.

What is style binding in angular?

Style Binding in Angular. Style binding is used to set a style of a view element. We can set inline styles with style binding. Like with class and attribute binding, style binding syntax is like property binding. In property binding, we only specify the element between brackets.

How to make my AngularJS application work on Internet Explorer 11?

To ensure your AngularJS application works on IE please consider: Use ng-style tags instead of style="{{ someCss }}". The latter works in Chrome, Firefox, Safari and Edge but does not work in Internet Explorer (even 11). For the type attribute of buttons, use ng-attr-type tags instead of type="{{ someExpression }}".

How to assign a variable to a component in angular?

Refer to a template variable anywhere in the component's template. Here, a <button> further down the template refers to the phone variable. Angular assigns a template variable a value based on where you declare the variable: If you declare the variable on a component, the variable refers to the component instance.


2 Answers

As a workaround you could use ng-style:

<div ng-style="{ 'margin-left': 50 + 'px'}">TEST2 {{50}}</div>

Fiddle: http://jsfiddle.net/YXe8Z/

like image 167
Jason Goemaat Avatar answered Oct 15 '22 10:10

Jason Goemaat


About the bug, it's strange that only IE behaves this way.
Anyway, you could ng-style directive for this purpose - because if you're going to wait until Angular parse that interpolation and correctly apply the style, it's better to do it with proper directives :)

<div ng-style="{ 'margin-left': 50 + 'px' }">TEST2</div>

http://jsfiddle.net/q4UKD/

like image 29
gustavohenke Avatar answered Oct 15 '22 09:10

gustavohenke