AngularJS expression not working within style attr

2019-01-21 21:53发布

Using an expression like this on the style attribute works on Chrome but doesn't work on IE8

style="width:{{progress}}%"

http://jsfiddle.net/5VDMD/12/ (to test it please type a number in the textbox)

Any workaround for this problem?

5条回答
SAY GOODBYE
2楼-- · 2019-01-21 22:28

Try

ng-style="{ width: progress + '%' }"
查看更多
相关推荐>>
3楼-- · 2019-01-21 22:32

if anybody use px instead of %, you have to use this

ng-style="{ width: progress + \'px\' }"
查看更多
老娘就宠你
4楼-- · 2019-01-21 22:34

For some reason in IE I had to use

ng-attr-style="{{METHOD_TO_RETURN_SOME_STYLE()}}"

Though mine was in a directive under an ng-repeat.

查看更多
Fickle 薄情
5楼-- · 2019-01-21 22:37

I made it work that way:

in the controller:

$scope.getStyle = function(progress){
    return {
        width: progress + "%"
    }
}

in the HTML:

<div class="progbar" ng-style="getStyle(progress)"></div>
查看更多
三岁会撩人
6楼-- · 2019-01-21 22:40

I had to use ng-attr-style (though I didn't need to use a function).

<div ng-attr-style="width: {{value}}%"></div>

Here is a github discussion about this issue.

查看更多
登录 后发表回答