Javascript 变量自身改变值?
我有一个简单的角度应用程序,它只是一个带有分页和排序的表。对表所做的更改(搜索、筛选、升序/降序排序等)反映在URL中。我使用ui.router来实现这一点 单击页码上的数字时,表会正确更新,URL会显示正确的页码。但是,如果我直接访问URL,它会在一秒钟内注册正确的号码,然后重置为1。它本身。它没有分配到任何地方 我要发疯了,想知道为什么会这样。其他一切都在初始URL加载时起作用。如果我在最初的URL访问中放置了一个要排序的搜索或列,它将正确地处理它。然而,分页似乎有自己的想法。我错过什么了吗?是否有理由将其自身设置回1 HTML:Javascript 变量自身改变值?,javascript,angularjs,pagination,angular-ui-router,Javascript,Angularjs,Pagination,Angular Ui Router,我有一个简单的角度应用程序,它只是一个带有分页和排序的表。对表所做的更改(搜索、筛选、升序/降序排序等)反映在URL中。我使用ui.router来实现这一点 单击页码上的数字时,表会正确更新,URL会显示正确的页码。但是,如果我直接访问URL,它会在一秒钟内注册正确的号码,然后重置为1。它本身。它没有分配到任何地方 我要发疯了,想知道为什么会这样。其他一切都在初始URL加载时起作用。如果我在最初的URL访问中放置了一个要排序的搜索或列,它将正确地处理它。然而,分页似乎有自己的想法。我错过什么了吗
<pagination items-per-page="25" total-items="117" ng-model="currentPage"></...>
app.config(['$stateProvider', '$urlRouterProvider',
function($stateProvider, $urlRouterProvider) {
$stateProvider.state('filter', {
url: '/column/:column/desc/:sort/search/:search/page/:page/',
parent: 'parentState',
controller: 'Ctrl'
});
}
]);
app.controller('Ctrl', function($scope, $state, $stateParams) {
$scope.currentPage = 1;
$scope.totalItems = 0;
$scope.$on('$stateChangeSuccess', function(evt, toState, toParams, fromState, fromParams) {
$scope.sorting.predicate = toParams.column;
$scope.sorting.reverse = boolVal(toParams.sort);
$scope.currentPage = Number(toParams.page);
$scope.searchBuffer = JSON.parse(toParams.search);
});
$scope.$watch('currentPage', function() {
$scope.currentRow = ($scope.currentPage - 1) * $scope.itemsPerPage;
$scope.updateURL();
}, true);
$scope.updateURL = function() {
$state.transitionTo('filter', {
column: $scope.sorting.predicate,
desc: $scope.sorting.reverse,
search: angular.toJson($scope.searchBuffer),
page: Number($scope.currentPage)
});
}
});
Iconsole.log
在我的代码中(未发布),发生的情况如下:
如果我转到URLwww.mydomain.com#/column/'columnname'/desc/false/search/{}/page/17/
,考虑到我的分页中至少有17页:
Iconsole.log
在currentPage$watch
中,currentPage的值是1(有道理,它是用我设置的值初始化的)。之后,它被记录为17。很棒,然后$viewContentLoaded会记录所有正确的$stateParams
。那么在currentPage再次被记录为1之后。。?为什么
另一方面,在初始加载之后,一切都按预期工作,除了在分页时更改页面,$stateChangeSuccess
运行两次。发生什么事了
编辑:
<pagination items-per-page="25" total-items="117" ng-model="currentPage"></...>
app.config(['$stateProvider', '$urlRouterProvider',
function($stateProvider, $urlRouterProvider) {
$stateProvider.state('filter', {
url: '/column/:column/desc/:sort/search/:search/page/:page/',
parent: 'parentState',
controller: 'Ctrl'
});
}
]);
app.controller('Ctrl', function($scope, $state, $stateParams) {
$scope.currentPage = 1;
$scope.totalItems = 0;
$scope.$on('$stateChangeSuccess', function(evt, toState, toParams, fromState, fromParams) {
$scope.sorting.predicate = toParams.column;
$scope.sorting.reverse = boolVal(toParams.sort);
$scope.currentPage = Number(toParams.page);
$scope.searchBuffer = JSON.parse(toParams.search);
});
$scope.$watch('currentPage', function() {
$scope.currentRow = ($scope.currentPage - 1) * $scope.itemsPerPage;
$scope.updateURL();
}, true);
$scope.updateURL = function() {
$state.transitionTo('filter', {
column: $scope.sorting.predicate,
desc: $scope.sorting.reverse,
search: angular.toJson($scope.searchBuffer),
page: Number($scope.currentPage)
});
}
});
我想我发现了一个错误:
当我完全从HTML中删除分页时,重置为1的操作就停止了。这让我得出这样的结论:当我试图更改页面时,分页可能没有准备好/没有加载所有数据,所以它返回到1。有人知道如何规避这种行为吗
即使我在转换状态时设置了很长的延迟(10秒),它仍然会重置为1。事件顺序与您所说的完全一样,除了最后一个:
$scope.currentPage = 1;
$scope.$on('$stateChangeSuccess',
$scope.currentPage = Number(toParams.page);
$scope.$watch('currentPage', function() {
$scope.updateURL();
$state.transitionTo(
$scope.currentPage = 1;
但是序列与您的流相匹配…我不熟悉分页库,但是您确定第一次加载页面时触发了
“$stateChangeSuccess”
事件吗?您将$scope.currentPage
初始化为1,但它仅在触发该事件时将其值更新为正确的页码。是的,它被初始化为1。然后调用$stateChangeSuccess
,加载正确的值。然后重置为1。所有这些都发生在初始页面加载时。重新加载后应该保持在17。甚至注释掉$scope.currentPage=1代码>仍然会导致17被重置为1。如果我将currentPage
的init更改为5,它将变为5,17,1。它应该只是变为initValue,17。没有重置为1。我怀疑Number($scope.currentPage)
方法,您没有粘贴到此处的代码中的某个地方导致了问题。请告诉我一件事。如果您将控制器默认值设置为11($scope.currentPage=11;),您会得到默认值2吗