简体   繁体   English

AngularJS ng-class无法在带有$ index的ng-repeat中工作

[英]AngularJS ng-class not working in ng-repeat with $index

I have a list created with ng-repeat that works for everything except for adding the ng-class on condition. 我有一个用ng-repeat创建的列表,除了在条件上添加ng-class之外,它适用于所有内容。

<div ng-repeat="glossary in glossarySections"
     class="alphabet"
     ng-click="glossaryGoToSection($index)"
     ng-class="{'selected',$index == $parent.glossarySection}">
    {{glossary.name}}
</div>

ng-click works, and it creates the DOM the way I expect it to, but it doesn't add the 'selected' class for the alphabet currently shown. ng-click可以正常工作,并且可以按我期望的方式创建DOM,但不会为当前显示的字母添加“ selected”类。

Is there something that I am missing in this syntax? 我在此语法中缺少什么吗?

There is a syntax error: replace , to : . 有一个语法错误:替换,:

Modify to ng-class="{'selected': $index == $parent.glossarySection}"> . 修改为ng-class="{'selected': $index == $parent.glossarySection}">

ngClass receives a hash object: class: condition ngClass接收一个哈希对象: class: condition

So you should use colon for key/condition separation: 因此,您应该使用冒号进行键/条件分离:

ng-class="{selected: $index==$parent.glossarySection}"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM