当我在单元格中单击时,如何使文本更改颜色

时间:2015-01-26 16:07:51

标签: angularjs

我想更改单元格的颜色,所以在我的表格中我会在td

上执行此操作
data-ng-class="{selected: selected.id == price.id && price.isMinPrice}"
  

如果被选中并且是我希望有颜色的最低价格

但是此代码行无效,为什么?

1 个答案:

答案 0 :(得分:1)

也许你需要这样的东西:

在这里,您可以在三元运算符中定义任何条件

data-ng-class="selected.id == price.id && price.isMinPrice ? 'selected-class' : ''"

您还可以为这两种情况添加更多类:

data-ng-class="some-condition ? 'selected-class' : 'no-selected-class another-class'"

完整示例:

angular.module('test', []);

angular.module('test').controller('controll', ['$scope',
  function($scope) {

    $scope.items = [{
      value: 1,
      selected: false
    }, {
      value: 2,
      selected: true
    }, {
      value: 3,
      selected: false
    }]

  }
]);
.selected-class {
  color: red;
}
<div ng-app="test">
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

  <div ng-controller="controll">
    <table style="width:100%">
      <tr ng-repeat="item in items">
        <td data-ng-class="item.selected ? 'selected-class' : ''">{{item.value}}</td>
      </tr>
    </table>

  </div>
</div>