访问另一个数组的一个数组的索引

时间:2017-04-27 06:19:20

标签: angularjs arrays

我想在dashboardCtrl.labelColors中访问dashboardCtrl.labels的索引,这是另一个数组。我尝试了以下但没有成功。如果我只打印{{$ index}},则会成功打印}}

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

2 个答案:

答案 0 :(得分:3)

您不需要嵌套括号{{}}。试试这个

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

答案 1 :(得分:2)

执行以下操作。只需$index {{dashboardCtrl.labelColors[$index]}}而不是{{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>

修改

在Angular JS中动态应用CSS样式属性

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