2

我将如何使用 AngularJS ng-repeat 来重复这个结构?

<div class=black></div>
<div class=white></div>
<div class=black></div>
<div class=white></div>

...等(不带标签)

4

3 回答 3

7

如果您正在迭代一系列项目并且需要交替的黑色/白色背景,那么您可以这样做:

<div ng-repeat="item in items" ng-class-odd="'black'" ng-class-even="'white'">
</div>

http://jsfiddle.net/fNCnr/1/

于 2013-01-08T18:29:25.970 回答
0
<div ng-repeat="color in ['black','white']" class="{{color}}"></div>

输出:

<div ng-repeat="color in ['black','white']" class="black"></div>
<div ng-repeat="color in ['black','white']" class="white"></div>

或者(更详细):

<div ng-repeat="color in ['black','white']" ng-class="color"></div>

输出:

<div ng-repeat="color in ['black','white']" ng-class="color" class="ng-scope black"></div>
<div ng-repeat="color in ['black','white']" ng-class="color" class="ng-scope white"></div>
于 2013-01-08T18:21:07.513 回答
0

在您的控制器中,您定义了一个颜色列表:

$scope.colors = ['black', 'white', 'black', 'white', 'black', 'white', ...];

html:

<div ng-repeat="color in colors" ng-class="color"></div>

请参阅jsFiddle

于 2013-01-08T18:23:24.693 回答