2017-06-15 34 views
0

我正尝试在ng-repeat中使用ng-class,并尝试将类应用于第一个单元格。我的第一个单元应该有一个红色背景。但是,它似乎没有任何影响。将css类应用于ng-repeat中的第一个单元格

<table> 
    <tbody> 
     <tr ng-repeat="item in items" class="item"> 
      <td class="thing" ng-repeat="thing in things" ng-class="{thing-red: $first}"></td> 
     </tr> 
    </tbody> 
</table> 

.thing-red { 
    background:red; 
} 

可正常工作(所有单元都红):

<td class="thing thing-red" ng-repeat="thing in things"></td> 
+2

是否在引号包裹的东西,红色的有效果?即'ng-class =“{'thing-red':$ first}”' – Will

+0

它的确如此!为什么我不试试:) – Prabhu

+1

是的,我认为它与'ng-class'的实现有关。这里引导我的经验法则是仅使用有效的对象键的类名称。我认为这是你需要报价时和不需要报价时的区别。 – Will

回答

1

试试下面的代码

<td class="thing" ng-repeat="thing in things" ng-class="{'thing-red': $index == 0}"></td> 
相关问题