所以我在使用ng-repeat將兩個數組項放入div標籤內時遇到了一些麻煩。嘗試了一些事情,但無法弄清楚。下面你可以看到我試圖達到的佈局......任何幫助都會很棒!可以在angularJS v1中每次使用ng-repeat項目兩次嗎?
angular.module('starter', [])
.controller('PeopleCtrl', function($scope) {
$scope.people = [
{
image: "https://placehold.it/350x150",
dob: "23/06/1990"
}, {
image: "https://placehold.it/350x150",
dob: "12/12/2000"
}, {
image: "https://placehold.it/350x150",
dob: "12/12/1972"
}
]
angular.forEach($scope.people, function(value, key) {
var dateParts = $scope.people[key].dob.split("/");
var date = new Date(dateParts[2], (dateParts[1] - 1), dateParts[0]);
$scope.people[key].dob = date;
})
});
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
<title></title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
</head>
<body ng-app="starter" ng-controller="PeopleCtrl">
<!-- I want to put 2 people in a row. Then the next 2 people in a row... -->
<div class="row" ng-repeat="person in people | orderBy:'-dob'">
<div class="col col-50">
<img ng-src="{{person.image}}">
<p>{{person.dob | date : 'dd MMM yyyy'}}</p>
</div>
<!-- Here I want the next item in the array, not the same one. -->
<div class="col col-50">
<img ng-src="{{person.image}}">
<p>{{person.dob | date : 'dd MMM yyyy'}}</p>
</div>
</div>
</body>
我應該提一下我使用離子的網格。 http://ionicframework.com/docs/components/#grid
爲什麼不是每個第二個項目後,剛拆?也許沿着這樣的路線:
。 你可以用你喜歡的任何方式使用它。 –'ng-repeat'內有一個名爲'$ index'的變量。你可以利用它和'ng-if'來有條件地輸出div – DrinkBird