AngularJS:ng-class - 使用多个类?

时间:2014-01-08 16:25:57

标签: javascript angularjs angularjs-ng-repeat ng-class

我是AngularJS的新手。

我想根据它的值为第二列添加一个类。示例:如果值为>,我想添加类'positive' 0和等级'负'当它是< 0

我目前使用的是:

<td data-ng-class="{positive: $index==1 && {{values}} > 0 , negative:{{values}} < 0 && $index==1 }" ng-repeat="values in portfolio" >{{values}}</td>

现在,这似乎有效(见图):

enter image description here

但是,我在我的控制台中收到以下内容:

错误:[$ parse:syntax] http://errors.angularjs.org/1.2.7/ $ parse / syntax?p0 =%26&amp; p1 = is%20unexpected%2C%20expecting%20%5B%7D%5D&amp; p2 = 26&amp; p3 = %7Bpositive%3A%20%24index%3D%3D1%20%26%26%20S%26P500%20%3E%200%20%2C%20negative%3AS%26P500%20%3C%200%20%26%26 %20%24index%3D%3D1%20%7D&安培; P4 =%26P500%20%3E%200%20%2C%20negative%3AS%26P500%20%3C%200%20%26%26%20%24index% 3D%3D1%20%7D     在错误()     在https://ajax.googleapis.com/ajax/libs/angularjs/1.2.7/angular.min.js:6:449     在Xa.throwError(https://ajax.googleapis.com/ajax/libs/angularjs/1.2.7/angular.min.js:155:346)     在Xa.consume(https://ajax.googleapis.com/ajax/libs/angularjs/1.2.7/angular.min.js:156:325)     在Xa.object(https://ajax.googleapis.com/ajax/libs/angularjs/1.2.7/angular.min.js:164:115)     在Xa.primary(https://ajax.googleapis.com/ajax/libs/angularjs/1.2.7/angular.min.js:154:482)     在Xa.unary(https://ajax.googleapis.com/ajax/libs/angularjs/1.2.7/angular.min.js:161:240)     在Xa.multiplicative(https://ajax.googleapis.com/ajax/libs/angularjs/1.2.7/angular.min.js:160:480)     在Xa.additive(https://ajax.googleapis.com/ajax/libs/angularjs/1.2.7/angular.min.js:160:325)     在Xa.relational(https://ajax.googleapis.com/ajax/libs/angularjs/1.2.7/angular.min.js:160:204

2 个答案:

答案 0 :(得分:2)

尝试:

<td data-ng-class="{positive: $index==1 && values > 0, negative:values < 0 && $index==1 }" ng-repeat="values in portfolio" >{{values}}</td>

答案 1 :(得分:0)

您应该将{{values}}更改为values

<td data-ng-class="{positive: $index==1 && values > 0 , negative:values < 0 && $index==1 }" ng-repeat="values in portfolio" >{{values}}</td>