使用ng-repeat对AngularJS进行分页

时间:2017-10-24 20:57:44

标签: javascript angularjs pagination

我是新手,我正试图在AngularJS上创建我的第一个分页

我有一个像这样的ng-repeat列表

ng-repeat="vehiculo in vehiculos | startFrom:currentPage*pageSize | limitTo:pageSize"

这是我的控制器

/* Controllers */
angular.module('vehiculoApp.controller')

// Controller listado de vehiculos
.controller('VehiculoListCtrl', ['$scope', 'Vehiculo','$log',function($scope, Vehiculo,$log) {
    $scope.orderProp = '-id';

    Vehiculo.getAll()
        .success(function(data, status, headers, config){
            $scope.vehiculos = data;
        });



$scope.currentPage = 0;
$scope.pageSize = 10;
$scope.vehiculos = [];
$scope.numberOfPages=function(){
    return Math.ceil($scope.vehiculos.length/$scope.pageSize);                
}
for (var i=0; i<45; i++) {
    $scope.vehiculos.push("Item "+i);
}

vehiculoApp.filter('startFrom', function() {
return function(input, start) {
    start = +start; //parse to int
    return input.slice(start);
}


});

我遇到过滤器问题,因为控制台说

Error: [$injector:unpr] Unknown provider: startFromFilterProvider <-  startFromFilter
http://errors.angularjs.org/1.2.9/$injector/unpr?p0=startFromFilterProvider%20%3C-<!-- ngRepeat: vehiculo in vehiculos | startFrom:currentPage*pageSize | limitTo:pageSize -->tartFromFilter
at https://www.indicemotor.com/app/lib/angular.js:78:12
at https://www.indicemotor.com/app/lib/angular.js:3546:19
at Object.getService [as get] (https://www.indicemotor.com/app/lib/angular.js:3673:39)
at https://www.indicemotor.com/app/lib/angular.js:3551:45
at Object.getService [as get] (https://www.indicemotor.com/app/lib/angular.js:3673:39)
at Parser.$filter (https://www.indicemotor.com/app/lib/angular.js:13880:24)
at Parser.filter (https://www.indicemotor.com/app/lib/angular.js:10006:19)
at Parser.filterChain (https://www.indicemotor.com/app/lib/angular.js:9997:51)
at Parser.statements (https://www.indicemotor.com/app/lib/angular.js:9972:30)
at Parser.parse (https://www.indicemotor.com/app/lib/angular.js:9851:46)

1 个答案:

答案 0 :(得分:0)

获取startFrom过滤器声明并将其移出控制器定义。

vehiculoApp.filter('startFrom', function() {
return function(input, start) {
    start = +start; //parse to int
    return input.slice(start);
});
/* Controllers */
...