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

时间:2014-08-08 20:48:18

标签: 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>

如何解决此错误:

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]

3 个答案:

答案 0 :(得分:26)

ng-class的工作原理如下:

<i class="fa" ng-class='{"fa-sortup" : x && y, "fa-b": a && b}'></i>

所以,你错过了花括号。

P.S。如果类名包含特殊字符,例如&#39; - &#39;,则必须将类名放在字符串"fa-sortup"中。

答案 1 :(得分:4)

当你的班级有&#34; - &#34;你必须用&#39;&#39;包装它们的角色,如果你有多个条件,就把它变成一个数组,就像这样:

<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>

答案 2 :(得分:1)

对我来说,在Apache Cordova的Visual Studio工具中,我通过添加';'来解决错误在末尾。所以:ng-class="{class : expression};"