gpt4 book ai didi

javascript - AngularJS 1.6 应用程序错误 : turning items pagination into a service does not work

转载 作者:行者123 更新时间:2023-11-29 23:12:13 25 4
gpt4 key购买 nike

我正在使用 Bootstrap 4 和 AngularJS v1.6.6 制作一个小型通讯录应用

该应用程序仅显示一个用户 JSON。由于 JSON 返回大量用户,因此该应用程序还具有分页功能。

作为应用程序 Controller 的一部分,分页功能运行良好,但由于我试图将它变成一项服务,所以它不再工作了。

查看应用程序的功能版本, Controller 内有分页 HERE .

应用 Controller :

// Create an Angular module named "contactsApp"
var app = angular.module("contactsApp", []);

// Create controller for the "contactsApp" module
app.controller("contactsCtrl", ["$scope", "$http", "$filter", "paginationService", function($scope, $http, $filter) {
var url = "https://randomuser.me/api/?&results=100&inc=name,location,email,cell,picture";
$scope.contactList = [];
$scope.search = "";
$scope.filterList = function() {
var oldList = $scope.contactList || [];
$scope.contactList = $filter('filter')($scope.contacts, $scope.search);
if (oldList.length != $scope.contactList.length) {
$scope.pageNum = 1;
$scope.startAt = 0;
};
$scope.itemsCount = $scope.contactList.length;
$scope.pageMax = Math.ceil($scope.itemsCount / $scope.perPage);
};

$http.get(url)
.then(function(data) {
// contacts arary
$scope.contacts = data.data.results;
$scope.filterList();

// Pagination Service
$scope.paginateContacts = function(){
$scope.pagination = paginationService.paginateContacts();
}

});
}]);

服务:

app.factory('paginationService', function(){
return {
paginateContacts: function(){
// Paginate
$scope.pageNum = 1;
$scope.perPage = 24;
$scope.startAt = 0;
$scope.filterList();

$scope.currentPage = function(index) {
$scope.pageNum = index + 1;
$scope.startAt = index * $scope.perPage;
};

$scope.prevPage = function() {
if ($scope.pageNum > 1) {
$scope.pageNum = $scope.pageNum - 1;
$scope.startAt = ($scope.pageNum - 1) * $scope.perPage;
}
};

$scope.nextPage = function() {
if ($scope.pageNum < $scope.pageMax) {
$scope.pageNum = $scope.pageNum + 1;
$scope.startAt = ($scope.pageNum - 1) * $scope.perPage;
}
};
}
}
});

在 View 中:

<div ng-if="pageMax > 1">
<ul class="pagination pagination-sm justify-content-center">
<li class="page-item"><a href="#" ng-click="prevPage()"><i class="fa fa-chevron-left"></i></a></li>
<li ng-repeat="n in [].constructor(pageMax) track by $index" ng-class="{true: 'active'}[$index == pageNum - 1]">
<a href="#" ng-click="currentPage($index)">{{$index+1}}</a>
</li>
<li><a href="#" ng-click="nextPage()"><i class="fa fa-chevron-right"></i></a></li>
</ul>
</div>

服务文件包含在项目中(在我看来是正确的)在 app.js 文件之后:

<script src="js/app.js"></script>
<script src="js/paginationService.js"></script>

我不是高级 AngularJS 用户,所以我不知道:缺少什么?

最佳答案

看来service需要在controller之前定义,否则无法正常注入(inject)。

因此您可以将 paginationService 移动到 app.js 中:

var app = angular.module("contactsApp", []);
app.factory('paginationService', function(){
//...
});
app.controller("contactsCtrl", ["$scope", "$http", "$filter", "paginationService", function($scope, $http, $filter) {
//...
});

或者将 Controller 移出到包含在 paginationServices.js 文件之后的单独文件中。

看看this plunker .尝试修改第 6 行 - 删除字符 5,即分隔多行注释的星号和斜线的空格。

关于javascript - AngularJS 1.6 应用程序错误 : turning items pagination into a service does not work,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53682728/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com