【发布时间】:2016-04-21 22:06:53
【问题描述】:
所以我在使用 ng-repeat 将两个数组项放在一个 div 标签内时遇到了很多麻烦。尝试了几件事,但无法弄清楚。您可以在下面看到我正在尝试实现的布局...任何帮助都会很棒!
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>
我应该提到我正在使用 ionic 的网格。 http://ionicframework.com/docs/components/#grid
【问题讨论】:
-
为什么不在每隔一个项目之后拆分?也许类似于:。你可以以任何你喜欢的方式使用它。
-
ng-repeat内部有一个名为$index的变量。您可以利用这一点以及ng-if有条件地输出 div
标签: javascript angularjs html angularjs-scope