我有一個ng重複,我想添加項目的數組(專輯陣列),它工作正常。我有顏色數組,它會爲專輯陣列的卡片元素添加不同的背景顏色。結果是不正確的,我試圖使顏色數組對象在同一個數組仍然得到錯誤的結果。任何幫助讚賞。這裏是CodePen。顏色數組應循環回來以等於專輯陣列中任意數量的專輯。使用角度嵌套ng-repeat:
function MainController(albumSerivce, $filter) {
'use strict';
var ctrl = this;
ctrl.albums = [];
ctrl.arrayColor = ['red', 'blue', 'green', 'yellow'];
}
.green {
background-color: rgb(75, 175, 79);
padding: 10px;
}
.red {
background-color: rgb(255, 86, 34);
padding: 10px;
}
.blue {
background-color: rgb(61, 121, 182);
padding: 10px;
}
.yellow {
background-color: rgb(255, 255, 0);
padding: 10px;
}
<body ng-app="app">
<div class="container-fluid" ng-controller="MainController as ctrl">
<div class="albums-container" ng-repeat="album in ctrl.albums" ng-cloak>
<div class="album {{color}}" ng-repeat="color in ctrl.arrayColor">
<img class="image" ng-src="{{album.url}}" alt="" />
<div class="title">{{album.title}}</div>
</div>
</div>
</div>
</body>
更新,我也試着這樣做。無法完全弄清楚。
<!-- Also tried doing different template with class included and using ng-if with modulus if this mightbe the better route? -->
<div ng-class="album green" ng-if="$index%">
<img class="image" ng-src="{{album.url}}" alt="" />
<div class="title">{{album.title}}</div>
</div>
<div ng-class="album red" ng-if="$index%">
<img class="image" ng-src="{{album.url}}" alt="" />
<div class="title">{{album.title}}</div>
</div>
<div ng-class="album blue" ng-if="$index%">
<img class="image" ng-src="{{album.url}}" alt="" />
<div class="title">{{album.title}}</div>
</div>
<div ng-class="album yellow" ng-if="$index%">
<img class="image" ng-src="{{album.url}}" alt="" />
<div class="title">{{album.title}}</div>
</div>
代碼筆拋出一個意外的標記錯誤我現在 –
嘗試@NarainMittal它。 – alphapilgrim
當專輯比彩色更多時應該發生什麼?它是否應該從顏色數組的開始迴繞?如果是這樣,爲什麼不在你的css類中使用類似於第n個子類型的css選擇器 –