我有一些数据,我可以使用 ng-repeat
将这些数据编译成 div
。我试图将它们分成两列,但找不到构建它的方法。
这是我的例子: ( jsFiddle )
html:
<div ng-controller="Ctrl">
<div class="left">
<div ng-repeat="item in data">{{item.value}}</div>
<!-- i've tried filter and failed -->
</div>
<div class="right">
<div ng-repeat="item in data">{{item.value}}</div>
</div>
</div>
js:
var app = angular.module('app', []);
function Ctrl($scope) {
$scope.data = [
{value: "a"},
{value: "b"},
{value: "c"},
{value: "d"},// trying to divide from here
{value: "e"},// and show the last part in other column
{value: "f"},
{value: "g"},
{value: "h"}
];
}
最佳答案
甚至还有更简洁的解决方案,但您必须使用 angular 1.2.1:https://jsfiddle.net/1fch1221/6/
<div ng-controller="Ctrl">
<div class="left">
<div ng-repeat="item in data" ng-if="$index < (data.length / 2)">{{item.value}}</div>
</div>
<div class="right">
<div ng-repeat="item in data" ng-if="$index >= (data.length / 2)">{{item.value}}</div>
</div>
关于javascript - 使用ng-repeat时如何将数据分成两半?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28899546/