10

I am using the following code for "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>

How do I fix this error :

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]
1
  • 1
    You need to wrap fa-sort-up/fa-sort-down/fa-sort in quotes... to make it valid and use {} to wrap it as object
    – PSL
    Aug 8, 2014 at 20:50

3 Answers 3

26

ng-class works like this :

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

So, you are missing the curly braces.

p.s. You have to put the class name inside a string "fa-sortup" if they contain special characters like '-'.

4

When your classes have the "-" character you must wrap them with '', and if you have more than one condition, make it an array, just like this:

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

For me, and in Visual Studio Tools for Apache Cordova, I resolved the error by adding a ';' at the end. So :ng-class="{class : expression};"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.