In my Web App, I'm using ng-class="{highlighted_category: selected === category}"
to highlight an entry when it is selected. Problems occur when I want to use e.g. pure-menu-selected
because it contains dashes which Angular does not allow for some reason.
Error: [$parse:syntax] Syntax Error: Token '-' is unexpected, expecting [:] at column 6 of the expression [{pure-menu-selected: selected === category}] starting at [-menu-selected: selected === category}].
What would be an elegant way to solve this? Why does Angular not allow dashes in ngClass?
In my Web App, I'm using ng-class="{highlighted_category: selected === category}"
to highlight an entry when it is selected. Problems occur when I want to use e.g. pure-menu-selected
because it contains dashes which Angular does not allow for some reason.
Error: [$parse:syntax] Syntax Error: Token '-' is unexpected, expecting [:] at column 6 of the expression [{pure-menu-selected: selected === category}] starting at [-menu-selected: selected === category}].
What would be an elegant way to solve this? Why does Angular not allow dashes in ngClass?
Share Improve this question asked Aug 13, 2015 at 9:34 Claas M.Claas M. 2673 silver badges11 bronze badges 1- Can you show us the full part of that AngularJS Code? – Praveen Kumar Purushothaman Commented Aug 13, 2015 at 9:35
2 Answers
Reset to default 9Use single quotes to quote the class name
ng-class="{'pure-menu-selected': selected === category}"
The reason you can't use dashes unquoted is simple and makes sense. It's essentially the key of an object. It's the same reason a variable name cannot contain dashes.
Say you tried to have a variable name a-b
. Does that mean a variable named a-b
or does it mean a
minus b
?
Angular is treating your class as variable and you cannot use dash in variable names. You can return class as string via function in example:
ng-class="{getHighlightedClass() : selected === category}".
Where getHighlightedClass() you mus to implement in your controller.
By the way you can bind class directly as string. In example:
ng-class="{'pure-menu-selected' : selected === category}".
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745121849a4612459.html
评论列表(0条)