Angularjs分页作为服务或工厂
我是否可以为分页创建一个服务或工厂,这样我就不必在每个需要分页的控制器中对其进行编码 我的页码如下: 控制器Angularjs分页作为服务或工厂,angularjs,Angularjs,我是否可以为分页创建一个服务或工厂,这样我就不必在每个需要分页的控制器中对其进行编码 我的页码如下: 控制器 $scope.Find = function(){ $scope.Tablelist = {}; $scope.ShowPagination = false; $http({ method: 'POST', url: "http://localhost/api/warehouse/matinclist.php", h
$scope.Find = function(){
$scope.Tablelist = {};
$scope.ShowPagination = false;
$http({
method: 'POST',
url: "http://localhost/api/warehouse/matinclist.php",
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
}
}).then(function(response){
$scope.Tablelist = response.data;
$scope.ShowPagination = true;
}, function(response){
$scope.ShowPagination = false;
console.log("failed");
})
}
$scope.itemsPerPage = 12;
$scope.currentPage = 0;
$scope.range = function() {
var rangeSize = 8;
var ps = [];
var start;
start = $scope.currentPage;
// console.log($scope.pageCount(),$scope.currentPage)
if ( start > $scope.pageCount()-rangeSize ) {
start = $scope.pageCount()-rangeSize+1;
}
for (var i=start; i<start+rangeSize; i++) {
if(i>=0)
ps.push(i);
}
return ps;
};
$scope.prevPage = function() {
if ($scope.currentPage > 0) {
$scope.currentPage--;
}
};
$scope.prevPageDisabled = function() {
return $scope.currentPage === 0 ? "disabled" : "";
};
$scope.pageCount = function() {
return Math.ceil($scope.Tablelist.length/$scope.itemsPerPage)-1;
};
$scope.nextPage = function() {
if ($scope.currentPage < $scope.pageCount()) {
$scope.currentPage++;
}
};
$scope.nextPageDisabled = function() {
return $scope.currentPage === $scope.pageCount() ? "disabled" : "";
};
$scope.setPage = function(n) {
$scope.currentPage = n;
};
$scope.Find=function(){
$scope.Tablelist={};
$scope.ShowPagination=false;
$http({
方法:“POST”,
url:“http://localhost/api/warehouse/matinclist.php",
标题:{
“内容类型”:“应用程序/x-www-form-urlencoded”,
}
}).然后(功能(响应){
$scope.Tablelist=response.data;
$scope.ShowPagination=true;
},功能(回应){
$scope.ShowPagination=false;
console.log(“失败”);
})
}
$scope.itemsPerPage=12;
$scope.currentPage=0;
$scope.range=函数(){
var rangeSize=8;
var ps=[];
var启动;
开始=$scope.currentPage;
//console.log($scope.pageCount(),$scope.currentPage)
如果(开始>$scope.pageCount()-rangeSize){
start=$scope.pageCount()-rangeSize+1;
}
对于(变量i=开始;i=0)
ps.push(i);
}
返回ps;
};
$scope.prevPage=函数(){
如果($scope.currentPage>0){
$scope.currentPage--;
}
};
$scope.prevPageDisabled=函数(){
返回$scope.currentPage==0?“已禁用”:“”;
};
$scope.pageCount=函数(){
返回Math.ceil($scope.Tablelist.length/$scope.itemsPerPage)-1;
};
$scope.nextPage=函数(){
如果($scope.currentPage<$scope.pageCount()){
$scope.currentPage++;
}
};
$scope.nextPageDisabled=函数(){
返回$scope.currentPage==$scope.pageCount()?“已禁用”:“”;
};
$scope.setPage=函数(n){
$scope.currentPage=n;
};
我希望创建一个工厂的分页,所以如果我需要在任何控制器中使用它,我只需要注入它们
请帮助我如何做。您可以将其作为一个指令来编写,就像care提供一些指导一样?这取决于您对指令的理解。如果您了解如何编写指令,那么很明显,您需要为此编写一个隔离作用域指令,以及应该向作用域传递什么,或者至少通过引用引导分页来确定。如果您没有足够的信心编写一个指令,您可以从使用bootstrap指令开始,看看它是如何工作的。您可以像编写指令一样编写它,并提供一些指导?这取决于您对指令的理解。如果您了解如何编写指令,那么很明显,您需要为此编写一个隔离作用域指令,以及应该向作用域传递什么,或者至少通过引用引导分页来确定。如果您没有足够的信心编写一个,可以从使用bootstrap指令开始,看看它是如何工作的。