I can't get my head around this.
I am displaying a collection of objects with ng-repeat
, like this:
<div ng-repeat="obj in objs">{{obj | json}}</div>
Then, I want to apply the class current
to one of these <div>
elements based on whether the object it represented is the “current object”; i.e., in practice, whether obj
points to the same thing as another scope variable current
.
I tried this:
<div ng-repeat="obj in objs" ng-class="{current: 'obj == current'}">{{obj | json}}, current: {{obj == current}}</div>
The funny thing is: the expression obj == current
is correctly evaluated in the body of the div, and is initially false for all of them, when the value of current
in the scope is null
. But the class is applied to all of them nevertheless, although it is supposed to depend on the exact same expression.
Whenever I change the value of current
to point to another object, the text inside the div updates accordingly, and changes to true
, but the class always stays applied.
What am I missing here? Why is the class applied even if the expression yields false
?
To play with it: JSFiddle.
The quotes are causing this to fail. It should be changed to:
ng-class="{'current': obj == current}"
Edited your JS Fiddle: http://jsfiddle.net/qXgJm/1/
The quotes were causing it to always return true, because the string was truthy
Example: This would evaluate to true, and log the '!!!!'
if ("hello world") {
console.log('!!!!');
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다