我将如何使用 AngularJS ng-repeat 来重复这个结构?
<div class=black></div>
<div class=white></div>
<div class=black></div>
<div class=white></div>
...等(不带标签)
我将如何使用 AngularJS ng-repeat 来重复这个结构?
<div class=black></div>
<div class=white></div>
<div class=black></div>
<div class=white></div>
...等(不带标签)
如果您正在迭代一系列项目并且需要交替的黑色/白色背景,那么您可以这样做:
<div ng-repeat="item in items" ng-class-odd="'black'" ng-class-even="'white'">
</div>
<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>
在您的控制器中,您定义了一个颜色列表:
$scope.colors = ['black', 'white', 'black', 'white', 'black', 'white', ...];
html:
<div ng-repeat="color in colors" ng-class="color"></div>
请参阅jsFiddle。