How do I compare with a string in ng-class?

怎甘沉沦 提交于 2019-12-05 14:26:13

As is stated in the comments, your class name should be surrounded by single quotes.

ng-class="{'active': orderProp == 'title'}">

This comparison is case sensitive.

Had the same issue when using ng-class. It refused to dynamically compute the class attribute even though the expression was successfully calculated.

And here is the workaround I've used for the ng-class statement:

Sort By: <a href="" ng-click="setOrder('title')" class="{{orderProp == 'title' ? 'active' : ''}}">Alphabetical</a>

instead of ng-class="{active: orderProp == 'title'} I've switched to class="{{orderProp == 'title' ? 'active' : ''}}"

I mapped each string to an int pagetile->1 code->2 + data-ng-class="{active:orderPropIdx==1};" Inside the controller I just do if pagetitle set active:orderPropIdx to 1 and so on

Maybe this is a bug in angular 1.3

I resolve in this way; 'ClassName':'{{Value}}'=='StringtoCompare', ...
ng-class="{ 'btn-danger' : '{{datasource.difficoltaRicetta}}'=='Difficile', 'btn-warning' :'{{datasource.difficoltaRicetta}}'=='Intermedia', 'btn-success' : '{{datasource.difficoltaRicetta}}'=='Facile'}"

The syntax for ng-class can be quite confusing sometimes. Try this.

ng-class="{true: 'active', false: ''}[orderProp === 'title']"

Include square brackets after the curly braces. Inside the square brackets, you can declare any expression, and declare your results (true/false) and the corresponding classes you want to apply (active). And be sure to use '===' in your expressions, signifying you want orderProp to be exactly equal to what you are comparing it against.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!