我正在尝试使用 ng-repeat 中的 $index 生成 css 类,以便我可以单独控制每个 dom 元素。
像这样:
我需要使用 item_{$index}, item_{{$index}} 尝试了很多不同的方法并用谷歌搜索了这个问题一段时间但没有答案
我还注意到 Angular 似乎不喜欢 ng-class 类名中的下划线(_)或破折号(-)。去除它们可以使条件发挥作用。这是否也是我在 ng-repeat 中生成类的问题?
谢谢
最佳答案
如果没有您的实际代码,很难找出问题所在,尽管我相信以下内容是正确的
item_{{ $index }}
I don't think angular really cares about underscores and dashes, so it could also be that you have another template renderer conflicting here (e.g. if you're using Flask, then it could be jinja2 interfering with your angular).
That being said, it feels like it might be cleaner not to define separate CSS classes for each $index. Instead, you should define the actual style classes that you want, and then just use them with ng-class.
So, e.g. if you have a few CSS classes (assuming that you want to do a grid): item-foo, item-bar. In this case, if you wanted item-foo, you can do:
<div ng-class="{item-foo: someVariable}">...</div>
同样,元素栏:
<div ng-class="{item-bar: !someVariable}">...</div>
不确定这是否是您想要做的,但这只是一个建议。查看更多关于 ngClass .
关于javascript - 在 angularjs ng-repeat 中使用 $index 生成 css 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28160693/