Srikanth
Srikanth

Reputation: 37

how to assign a width to span dynamically

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

Upvotes: 2

Views: 1354

Answers (2)

Pavel
Pavel

Reputation: 668

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>

Upvotes: 1

Rebornix
Rebornix

Reputation: 5270

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>

Upvotes: 2

Related Questions