我需要添加过滤器来处理现有应用中的分页
在index.js中我们拥有所有
require('angular')
require('angular-ui-notification');
var myapp= angular.module('myapp', [
'ngRoute',
'ui.bootstrap',
'ngSanitize',
'ui.bootstrap'
]);
$http.get('configuration.json').then(function (response) {
var parameters = {};
parameters = response.data;
angular.element(document).ready(function() {
mainModule
.controller('SimpleCtrl', require('./controllers/Simple.js'))
.provider('runtimeConfig', require('./services/configuration.js'))
.config(function ($httpProvider) {
$httpProvider.defaults.headers.common["X-Requested-With"] = 'XMLHttpRequest';
})
.config(function(runtimeConfigProvider){
})
.config(function ($routeProvider) {
$routeProvider
.when('/', {
template: require('../views/myHome.html'),
controller: require('./controllers/myHome.js'),
accessPolicy: { authentication: false, role: "ALL"}
})
})
})
})
对于相应的myHome.html,我需要在myHome.js文件中添加分页,
'use strict';
module.exports = function($scope, $filter, $log, $route, $rootScope, $location, uibModal, MyService) {
}
我计划使用以下代码。
var app=angular.module('myApp', []);
function MyCtrl($scope) {
$scope.currentPage = 0;
$scope.pageSize = 10;
$scope.data = [];
$scope.numberOfPages=function(){
return Math.ceil($scope.data.length/$scope.pageSize);
}
for (var i=0; i<45; i++) {
$scope.data.push("Item "+i);
}
}
//We already have a limitTo filter built-in to angular,
//let's make a startFrom filter
app.filter('startFrom', function() {
return function(input, start) {
start = +start; //parse to int
return input.slice(start);
}
});
<link href="http://getbootstrap.com/2.3.2/bootstrap.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-controller="MyCtrl">
<ul>
<li ng-repeat="item in data | startFrom:currentPage*pageSize | limitTo:pageSize">
{{item}}
</li>
</ul>
<button ng-disabled="currentPage == 0" ng-click="currentPage=currentPage-1">
Previous
</button>
{{currentPage+1}}/{{numberOfPages()}}
<button ng-disabled="currentPage >= data.length/pageSize - 1" ng-click="currentPage=currentPage+1">
Next
</button>
</div>
但我不知道如何在myHome.js中注入特定的过滤器代码,
app.filter('startFrom', function() {
return function(input, start) {
start = +start; //parse to int
return input.slice(start);
}
});
我在index.js中有我的应用程序,需要从myHome.js进行分页。请建议如何将代码用于myHome.js。如果我尝试使用startFrom,
,我将得到以下异常bundle.annotated.js:29401 Error: [$injector:unpr] Unknown provider:
startFromFilterProvider <- startFromFilter
http://errors.angularjs.org/1.4.7/$injector/unpr?p0=startFromFilterProvider%20%3C-<div ng-view="" class="ng-scope">tartFromFilter
at http://localhost:9000/bundle.annotated.js:16992:12
at http://localhost:9000/bundle.annotated.js:21213:19
at Object.getService [as get] (http://localhost:9000/bundle.annotated.js:21361:39)
at http://localhost:9000/bundle.annotated.js:21218:45
at Object.getService [as get] (http://localhost:9000/bundle.annotated.js:21361:39)
at http://localhost:9000/bundle.annotated.js:35153:24
at new module.exports (http://localhost:9000/bundle.annotated.js:550:18)
at invoke (http://localhost:9000/bundle.annotated.js:21402:17)
at Object.instantiate (http://localhost:9000/bundle.annotated.js:21410:27)
at http://localhost:9000/bundle.annotated.js:26075:28
答案 0 :(得分:0)
要在控制器中添加为依赖项的过滤器的语法是
filterName+Filter
Eg: testFilter, reverseFilter, startFromFilter
根据我们的离线讨论,您的过滤器可以转到
mainModule
.filter('startFrom', function() {
return function(input, start) {
start = +start; //parse to int
return input.slice(start);
}
})
var app=angular.module('myApp', []);
app.controller('MyCtrl', ['$scope', '$filter', function ($scope, $filter) {
$scope.currentPage = 0;
$scope.pageSize = 10;
$scope.data = [];
$scope.q = '';
$scope.getData = function () {
// needed for the pagination calc
// https://docs.angularjs.org/api/ng/filter/filter
return $filter('filter')($scope.data, $scope.q)
}
$scope.numberOfPages=function(){
return Math.ceil($scope.getData().length/$scope.pageSize);
}
for (var i=0; i<45; i++) {
$scope.data.push("Item "+i);
}
}]);
//We already have a limitTo filter built-in to angular,
//let's make a startFrom filter
app.filter('startFrom', function() {
return function(input, start) {
start = +start; //parse to int
return input.slice(start);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.21/angular.min.js"></script>
<div ng-app="myApp" ng-controller="MyCtrl">
<input ng-model="q" id="search" class="form-control" placeholder="Filter text">
<select ng-model="pageSize" id="pageSize" class="form-control">
<option value="5">5</option>
<option value="10">10</option>
<option value="15">15</option>
<option value="20">20</option>
</select>
<ul>
<li ng-repeat="item in data | filter:q | startFrom:currentPage*pageSize | limitTo:pageSize">
{{item}}
</li>
</ul>
<button ng-disabled="currentPage == 0" ng-click="currentPage=currentPage-1">
Previous
</button>
{{currentPage+1}}/{{numberOfPages()}}
<button ng-disabled="currentPage >= getData().length/pageSize - 1" ng-click="currentPage=currentPage+1">
Next
</button>
</div>