javascript - 在 angularjs ng-repeat 中使用 $index 生成 css 类

标签 javascript css angularjs angularjs-ng-repeat

我正在尝试使用 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/

相关文章:

javascript - 使用 jQuery 或 JavaScript 检查输入/类型文本是否为空/null

css - 仅倾斜 div 的一个 Angular

html - float 列表问题

javascript - AngularJS $scope 原型(prototype)继承 - 原始与对象?

javascript - 如何将模拟服务注入(inject)过滤器的单元测试?

javascript - 将 JavaScript 的变量放入隐藏的输入类型

javascript - 等待 Angular 编译然后加载页面 [AngularJS]

javascript - 在函数中运行组合参数

html - 如何将 "scoreBox"内的元素水平居中?

angularjs - $http header 添加到 Access-Control-Request-Headers 下