angularjs - 在 "ng-style"迭代中使用 AngularJs "ng-repeat"

标签 angularjs angularjs-ng-repeat ng-style

我尝试使用 ng-style 根据数据元素的值有条件地设置表中数据元素的颜色。每行数据都是使用 ng Repeat 生成的。

所以我有类似的东西:

<tr ng-repeat="payment in payments">
  <td ng-style="set_color({{payment}})">{{payment.number}}</td>

我的 Controller 中的一个函数执行以下操作:

$scope.set_color = function (payment) {
  if (payment.number > 50) {
    return '{color: red}'
  }
}

我尝试了几种不同的方法。甚至将颜色设置为支付对象中的数据属性,但是似乎我无法让 ng-style 来处理来自数据绑定(bind)的数据, 有谁知道我可以做这个工作的方法吗?谢谢。

最佳答案

不要在 Angular expression 中使用 {{}} :

<td ng-style="set_color(payment)">{{payment.number}}</td>

从函数返回一个对象,而不是字符串:

$scope.set_color = function (payment) {
  if (payment.number > 50) {
    return { color: "red" }
  }
}

Fiddle

关于angularjs - 在 "ng-style"迭代中使用 AngularJs "ng-repeat",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16111994/

相关文章:

javascript - Angular 资源 promise 捕获, Jasmine toThrow()

javascript - 将代码移至 Angular 应用程序中的工厂中

javascript - AngularJS:在 ng-repeat 内调用 Controller 函数

javascript - Angular JS : inline style with bound value works on mac not windows

css - 如何转换样式 ="{{data.something}}"以显示在 IE 中?

javascript - Jade 中 Angular.JS 中 $scope 的问题

angularjs - TypeScript/angularJS HTTP GET 请求中的范围

javascript - 没有工作功能 ng-class 和 ng-repeat

javascript - angularjs $parse 字符串到 "controller as"方法

angularjs - ng-style 在嵌套的 ng-repeat 中不起作用