2

I am new to html and angularjs. Here I am trying to assign span width dynamically from angular js responce

<div class="statarea" ng-repeat="x in names">
     <div class="ratingarea">
        <div class="your-rating"><span style="width:"+{{ x.width }}+";" class="outer"></span>
        </div>
     <div class="clear"></div>
     </div>
</div>

In my Script is

$http.get(url).success(function (response) {
  $scope.names = response;
});

my response is

[
 {"width":"78%"},
 {"width":"60%"},
 {"width":"50%"}
]

I know it is not write,but i don't have any idea.. Thanks in advance

1

2 Answers 2

2

You can use ngStyle as other Angular built-in directives, for instance

ng-style="{color: myColor}"

ngStyle needs an Expression which evals to an object whose keys are CSS style names and values are corresponding values for those CSS keys. So in your case, you can set the width like

<div class="statarea" ng-repeat="x in names">
  <span ng-style="x" />
</div>

Or use scope variables like

<div class="statarea" ng-repeat="x in names">
  <span ng-style="{'width': x.width}" />
</div>
0
1

Your need to use ng-style. Here is your example in JSFiddle http://jsfiddle.net/2raw9xhq/2/

<div ng-app="" ng-init="names = [{width:'78%', 'background-color': 'green'},{width:'60%', 'background-color': 'red'},{width:'50%','background-color': 'blue'}]">
<div class="statarea" ng-repeat="x in names">
     <div class="ratingarea">
        <div class="your-rating">
            <span ng-style="x" class="outer">{{x.width}}</span>
        </div>
    </div>
    <div class="clear"></div>
</div>

0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.