0

I display a list of bars in a NgRepeat and I use the value frecuency to display the width of bars in percentage. From what i see IE 9-10 doesn't like this part: style="width:{{type.frecuency}}%;"

<div class="drp" ng-repeat="type in weeks">
    <div style="width:{{type.frecuency}}%;" class="percentBar">
        <span ng-if="type.frecuency > 14">{{type.frecuency}}%</span>
    </div>
</div>

Is this an issue with Angular on IE or my code is the problem. Thanks

P.S. I know that i could make a class but modifying the style attribute is faster.

Solution: ng-style="setBarWidth(type.frecuency);"

     scope.setBarWidth = function(width) {
        return {width: width+'%'}; 
     };
3
  • 4
    Does it work with ng-style? Commented Jul 18, 2014 at 12:29
  • +1 ivami, try with ng-style Commented Jul 18, 2014 at 12:31
  • Thanks guys! i got it work with: ng-style="setBarWidth(type.frecuency);" Commented Jul 18, 2014 at 12:38

1 Answer 1

1

When using derived values for various HTML attributes, it's always a good idea to use the provided Angular directives to do it. They make sure that the browser sees the values you want it to see and not the binding syntax (in your case {{type.frecuency}})

Here, the ngStyle directive should be used.

<div class="drp" ng-repeat="type in weeks">
    <div ng-style="width:{{type.frecuency}}%;" class="percentBar">
        <span ng-if="type.frecuency > 14">{{type.frecuency}}%</span>
    </div>
</div>

There are similar directives for many other HTML attributes, see the documentation for the full list.

Sign up to request clarification or add additional context in comments.

2 Comments

actually ng-style="width:{{type.frecuency}}%;" it won't work in any browser because angular will generate only ng-style="width:73.13%;" without the style attr
According to the docs, you should pass an object to ngStyle, i.e.: ng-style="{ width: type.frequency }". This seems to work well. Of course, you can also define the object in your code elsewhere, e.g. $scope.styles = { width: type.frequency };, and in the view just do ng-style="styles".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.