2

I want to access the index of dashboardCtrl.labels in dashboardCtrl.labelColors which is another array.I have tried the following but no success. If I print just {{$index}} it is getting printed successfully}}

<div class="row" ng-repeat="i in dashboardCtrl.labels">
                              <div id="circle" style="background:green"></div>{{i}}
                              <label>{{dashboardCtrl.labelColors[{{$index}}]}}</label>
                          </div>
1
  • 1
    try just index not {{index}} Commented Apr 27, 2017 at 6:20

2 Answers 2

3

You don't need nested brackets {{ }}. Try this

<div class="row" ng-repeat="i in dashboardCtrl.labels">
    <div id="circle" style="background:green"></div>{{i}}                              
    <label>{{dashboardCtrl.labelColors[$index]}}</label>
</div>
Sign up to request clarification or add additional context in comments.

Comments

2

Do the following. Just $index in {{dashboardCtrl.labelColors[$index]}} instead of {{dashboardCtrl.labelColors[{{$index}}]}}

<div class="row" ng-repeat="i in dashboardCtrl.labels">
    <div id="circle" style="background:green"></div>{{i}}
    <label>{{dashboardCtrl.labelColors[$index]}}</label>
</div>

EDIT

Apply CSS style attribute dynamically in Angular JS

ng-style="{'background-color': dashboardCtrl.labelColors[$index]}"

2 Comments

Yes that solved my problem is this the same way to assign style dynamically <div id="circle" style="background:{{dashboardCtrl.labelColors[$index]}}"></div>{{i}}
we use ng-style for that. I am adding a small demo in the answer's edit part.

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.