javascript 嵌套 ng-repeat 中的 Angular JS ng-class-odd

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18725832/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 12:59:14  来源:igfitidea点击:

Angular JS ng-class-odd in nested ng-repeats

javascriptangularjsnestedangularjs-ng-repeat

提问by Wolfman Joe

I'm trying to develop a very generic table outputter - no set number of rows or columns. As such, I've got nested ng-repeat attributes, as such:

我正在尝试开发一个非常通用的表输出器 - 没有固定的行数或列数。因此,我有嵌套的 ng-repeat 属性,如下所示:

<table>
    <tr ng-repeat="row in rowList">
        <td ng-repeat="col in colList">{{printCell(row,col)}}</td>
    </tr>
</table>

It's working great! Until I try to use ng-class-evenand ng-class-oddto change the background color of the rows accordingly.

它工作得很好!直到我尝试使用ng-class-even并相应ng-class-odd地更改行的背景颜色。

If I put the ng-class-***statements on the tdtag, I get alternating column colors.

如果我将ng-class-***语句放在td标签上,我会得到交替的列颜色。

If I put the ng-class-***statements on the trtag, I don't get anyclass assignment - they all stay default.

如果我把这些ng-class-***语句放在tr标签上,我不会得到任何课堂分配——它们都保持默认。

I want alternating row colors. How do I go about doing this?

我想要交替的行颜色。我该怎么做?

EDIT:

编辑:

Please delete this, someone? It turns out the problem was that my css classes were specifying that the class were set on a td tag.

请删除这个,有人吗?事实证明,问题在于我的 css 类指定该类设置在 td 标签上。

回答by TheSharpieOne

The value of ng-class-oddand ng-class-evencan be a string: ng-class-odd="'myClass'"or an expression ng-class-odd="{myClass: boolExpression}"

的值ng-class-oddng-class-even可以是字符串:ng-class-odd="'myClass'"或表达ng-class-odd="{myClass: boolExpression}"

Also:

还:

Angular 1.2+: ng-class="{even: $even, odd: $odd}"

角 1.2+: ng-class="{even: $even, odd: $odd}"

<table>
    <tr ng-repeat="row in rowList" ng-class="{even: $even, odd: $odd}">
        <td ng-repeat="col in colList">{{printCell(row,col)}}</td>
    </tr>
</table>
<hr />

Angular < 1.2 ng-class="{even: !($index%2), odd: ($index%2)}"

角度 < 1.2 ng-class="{even: !($index%2), odd: ($index%2)}"

<table>
    <tr ng-repeat="row in rowList" ng-class="{even: !($index%2), odd: ($index%2)}">
        <td ng-repeat="col in colList">{{printCell(row,col)}}</td>
    </tr>
</table>

Examples: http://jsfiddle.net/TheSharpieOne/JYn7S/1/

示例:http: //jsfiddle.net/TheSharpieOne/JYn7S/1/