admin管理员组文章数量:1421542
I want to highlight li
if that radio
is selected.
Inside ng-repeat
, ng-class
true
condition is working, but false
condition is not working, please check the code below
<div ng-init="friends = [
{name:'John', age:25, gender:'boy'},
{name:'Jessie', age:30, gender:'girl'},
{name:'Johanna', age:28, gender:'girl'},
{name:'Joy', age:15, gender:'girl'},
{name:'Mary', age:28, gender:'girl'},
{name:'Peter', age:95, gender:'boy'},
{name:'Sebastian', age:50, gender:'boy'},
{name:'Erika', age:27, gender:'girl'},
{name:'Patrick', age:40, gender:'boy'},
{name:'Samantha', age:60, gender:'girl'}
]">
<ul class="example-animate-container">
<li class="animate-repeat" ng-repeat="friend in friends" ng-class="{true:'hightlight',false:''}[radioBox==friend.name]">
<input type="radio" name="friends" ng-model="radioBox" value="{{friend.name}}" />
</li>
</ul>
</div>
I want to highlight li
if that radio
is selected.
Inside ng-repeat
, ng-class
true
condition is working, but false
condition is not working, please check the code below
<div ng-init="friends = [
{name:'John', age:25, gender:'boy'},
{name:'Jessie', age:30, gender:'girl'},
{name:'Johanna', age:28, gender:'girl'},
{name:'Joy', age:15, gender:'girl'},
{name:'Mary', age:28, gender:'girl'},
{name:'Peter', age:95, gender:'boy'},
{name:'Sebastian', age:50, gender:'boy'},
{name:'Erika', age:27, gender:'girl'},
{name:'Patrick', age:40, gender:'boy'},
{name:'Samantha', age:60, gender:'girl'}
]">
<ul class="example-animate-container">
<li class="animate-repeat" ng-repeat="friend in friends" ng-class="{true:'hightlight',false:''}[radioBox==friend.name]">
<input type="radio" name="friends" ng-model="radioBox" value="{{friend.name}}" />
</li>
</ul>
</div>
Share
Improve this question
edited Aug 18, 2014 at 10:13
jay_t55
11.7k28 gold badges108 silver badges176 bronze badges
asked Aug 18, 2014 at 10:08
sameersameer
3203 silver badges14 bronze badges
2 Answers
Reset to default 4This is because the ng-repeat
create a new child scope for each li
element, thus every li
element will has its own radioBox
value in its own scope.
For more detail, read Understanding-Scopes.
You could avoid this problem by having a .
in your ng-model
, for example using model.radioBox
like this:
<li class="animate-repeat" ng-repeat="friend in friends" ng-class="{true:'hightlight',false:''}[model.radioBox==friend.name]">
<input type="radio" name="friends" ng-model="model.radioBox" value="{{friend.name}}" />
</li>
And in your controller, initialize the model
beforehand:
$scope.model = {
radioBox: undefined
};
Example Plunker: http://plnkr.co/edit/TcOrxhSzYtN2KCxF2M3g?p=preview
If the expression evaluates to an object, then for each key-value pair of the object with a truthy value the corresponding key is used as a class name.
ng-class="{'highlight':radioBox==friend.name}"
本文标签: javascriptAngular js ngclass false condition not working in ngrepeatStack Overflow
版权声明:本文标题:javascript - Angular js ng-class false condition not working in ng-repeat - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745352298a2654841.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论