angularjs - ng-class - [$ parse:syntax]语法错误:令牌':'是表达式第11列的意外标记

Foo*_*ife 9 angularjs ng-class

我使用以下代码"ng-class":

 <i class="fa" ng-class="fa-sort-up:tableService.sortState.sortBy==id && tableService.sortState.sortMode=='asc',
                               fa-sort-down:tableService.sortState.sortBy==id && tableService.sortState.sortMode=='desc',
                               fa-sort:tableService.sortState.sortBy!=id"></i>
Run Code Online (Sandbox Code Playgroud)

如何修复此错误:

Error: [$parse:syntax] Syntax Error: Token ':' is an unexpected token at column 11 of the expression [fa-sort-up:tableService.sortState.sortBy==id && tableService.sortState.sortMode=='asc',
fa-sort-down:tableService.sortState.sortBy==id && tableService.sortState.sortMode=='desc',
fa-sort:tableService.sortState.sortBy!=id] starting at [:tableService.sortState.sortBy==id && tableService.sortState.sortMode=='asc',
fa-sort-down:tableService.sortState.sortBy==id && tableService.sortState.sortMode=='desc',
fa-sort:tableService.sortState.sortBy!=id]
Run Code Online (Sandbox Code Playgroud)

az7*_*7ar 26

ng-class的工作原理如下:

<i class="fa" ng-class='{"fa-sortup" : x && y, "fa-b": a && b}'></i>
Run Code Online (Sandbox Code Playgroud)

所以,你错过了花括号.

ps如果类名"fa-sortup"包含特殊字符(如' - '),则必须将类名放在字符串中.