编辑:重置/ TypeError是由于未在路由中指定reloadOnSearch: false
。对于尝试将页面重定向到默认值(1
)的分页,告诉页面加载$timeout
中的结果解决了这个问题。
我目前在angularJS中遇到了相当恼人的错误。
我试图获得&设置特定的搜索参数:
var searchParams = $location.search();
$scope.limit = searchParams.limit > 0 ? searchParams.limit : 10;
$scope.currentPage = searchParams.page > 0 ? searchParams.page : 1;
这样可以使参数正常,但是如果我使用除默认值(10和1)之外的任何参数加载页面,它将使用默认值自动重新加载(触发控制器两次)。
此外,当我自己设置搜索查询时:
$scope.changePage = function() {
$location.search('page', $scope.currentPage);
getUsers();
};
导致以下TypeError: Cannot read property 'add' of undefined
错误:
TypeError: Cannot read property 'add' of undefined
at resolveElementClasses (http://localhost:8080/site/bower_components/angular-animate/angular-animate.js:480:22)
at http://localhost:8080/site/bower_components/angular-animate/angular-animate.js:1028:27
at http://localhost:8080/site/bower_components/angular-animate/angular-animate.js:469:22
at Scope.$digest (http://localhost:8080/site/bower_components/angular/angular.js:13324:36)
at Scope.$apply (http://localhost:8080/site/bower_components/angular/angular.js:13540:24)
at HTMLAnchorElement.<anonymous> (http://localhost:8080/site/bower_components/angular/angular.js:21697:23)
at HTMLAnchorElement.eventHandler (http://localhost:8080/site/bower_components/angular/angular.js:2986:21)
(anonymous function) angular.js:10683
(anonymous function) angular.js:7858
Scope.$digest angular.js:13326
Scope.$apply angular.js:13540
(anonymous function) angular.js:21697
eventHandler angular.js:2986
这里是完整的控制器 - 用分页加载用户 - 理论上没什么复杂的:
app.controller('UserCtrl', ['$scope', '$location', 'User', function ($scope, $location, User) {
// Get from URL if present, else set defaults
var searchParams = $location.search();
$scope.limit = searchParams.limit > 0 ? searchParams.limit : 10;
$scope.currentPage = searchParams.page > 0 ? searchParams.page : 1;
var getUsers = function() {
$scope.loading = true;
$scope.users = null;
User.one().get({
'limit': $scope.limit,
'offset': ($scope.currentPage - 1) * $scope.limit
}).then(function(resp) {
$scope.loading = false;
$scope.users = resp.data;
$scope.users_count = resp._meta.total_count;
});
};
getUsers();
$scope.changePage = function() {
$location.search('page', $scope.currentPage);
getUsers();
};
}]);
我在这里看不到任何错误,但也许我错过了什么。
http:/site/users?page=2
会触发,然后立即使用http://site/users?page=1
重新加载TypeError
。