如何在Angular中使用布尔模型属性切换类?

时间:2013-07-08 10:22:21

标签: javascript angularjs

我试过了:

<li ng-repeat="todo in todos" class="{{todo.isCompleted ? 'test'}}">
<input type="checkbox" class="toggle"/>
<label>{{todo.text}}</label><button class="destroy"></button>
</li>

但后来我收到了一个错误:

Error: Lexer Error: Unexpected next character at columns 17-17 [?] in expression [todo.isCompleted ? 'test']

2 个答案:

答案 0 :(得分:6)

像这样使用ng-class

<li ng-repeat="todo in todos" ng-class="{ 'test' : todo.isCompleted }">

Reference

答案 1 :(得分:2)

ng-class="{ 'test' : todo.isCompleted }"