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

杀马特。学长 韩版系。学妹 提交于 2019-12-01 08:02:13

问题


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:

<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-even and ng-class-odd to change the background color of the rows accordingly.

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

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

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.


回答1:


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

Also:

Angular 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)}"

<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/



来源:https://stackoverflow.com/questions/18725832/angular-js-ng-class-odd-in-nested-ng-repeats

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!