【发布时间】:2016-01-19 20:43:59
【问题描述】:
我有一个名为 items 的数组,我试图通过一系列项目进行 ng-repeat,例如 0-4 或 5-11 等。所以我尝试创建自己的自定义过滤器,但目前是只显示所有数组元素。有更多知识的人可以看看它。
html
<div ng-app='myApp' ng-controller="Main">
<li ng-repeat="item in items | filter:range(0,2)">test {{item}}</li>
</div>
过滤器
var myApp = angular.module('myApp', []);
myApp.controller('Main', function($scope, $http) {
$scope.items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
$scope.range = function(min, max, step) {
return function(items) {
var arr = $scope.items;
step = step || 1;
var input = [];
for (var i = min; i <= max; i += step) {
if (arr[i] != null) {
input.push(arr[i]);
}
}
return items = input;
};
};
});
结果
test 1
test 2
test 3
test 4
test 5
test 6
test 7
test 8
test 9
test 0
【问题讨论】:
标签: javascript arrays angularjs filter ng-repeat