Angularjs标记表行或列取决于值

时间:2015-02-05 06:49:04

标签: javascript html css angularjs angularjs-ng-class

有没有办法在行或列上添加类取决于值。如果列“anzahl”为空,则行背景颜色必须为“灰色”,而“anzahl”列背景颜色为“红色”。

    <body ng-app ng-init='articles = [
   {"id":"1","name":"A&R","type":"P77 ","anzahl":"0"},
   {"id":"2","name":"Accuphase","type":"AC5 ","anzahl":"1"},
   {"id":"3","name":"Acoustical Systems","type":"Archon","anzahl":"1"}
  ]'>
  <div class="container">


    <table class="table">
      <tr ng-repeat="article in articles">
        <td>{{article.name}}</td>
        <td>{{article.type}}</td>
        <td>{{article.anzahl}}</td>
      </tr>
    </table>

  </div>

</body>

jsfiddle

2 个答案:

答案 0 :(得分:3)

您可以在案例中使用ng-class="{'class': expession}"

CSS

.custom-class{
  background-color: gray
}

.default-color{
  background-color: red
}


<table class="table">
  <tr ng-repeat="article in articles" ng-class="{'custom-class': article.anzahl == null, 'default-color': article.anzahl != null}">
    <td>{{article.name}}</td>
    <td>{{article.type}}</td>
    <td>{{article.anzahl}}</td>
  </tr>
</table>

感谢。

答案 1 :(得分:1)

这是我固定的小提琴,它是如何运作的。

.custom-class{
  background-color: gray
}

.default-color{
  background-color: white
}

.warning-class {
  background-color: red  
}


<table class="table">
  <tr ng-repeat="article in articles" ng-class="{'custom-class': article.anzahl === '0', 'default-color': article.anzahl !== '0'}">
    <td>{{article.name}}</td>
    <td>{{article.type}}</td>
    <td ng-class="{'warning-class': article.anzahl === '0', 'default-color': article.anzahl !== '0'}">{{article.anzahl}}</td>
  </tr>
</table>

fiddle