user3493022
user3493022

Reputation: 25

i want ng-style set to an array to which has different index

Initially I set my ng-style to something like:

<div ng-repeat="row in rowArray">
    <span ng-style="my_array[i]" >Some Text </span>
</div>

In the final i need something like,

<div> 
   <span ng-style="my_array[0]" >Some Text </span>
   <span ng-style="my_array[1]" >Some Text </span>
   <span ng-style="my_array[2]" >Some Text </span> 
</div>

So that i can change these styles in the .js file. How can i do this ?

Upvotes: 0

Views: 1066

Answers (1)

Antonio Laguna
Antonio Laguna

Reputation: 9300

The thing you mention is entirely possible with the use of $index:

<ul ng-repeat="word in words">
  <li ng-style="styles[$index]">{{ word }}</li>
</ul>

And then:

 $scope.styles = [
    {
      color: 'red'
    },
    {
      color: 'blue'
    }
 ];

Will do just fine. Here is a plunkr for you: http://plnkr.co/edit/aNxWtmwkbklQUUtok62C

Upvotes: 1

Related Questions