2013-04-29 110 views
3

UI路由器我使用下面做寻呼我的DataGrid:

$location.search('page', page); 

其中页是当前页码。

然后我听以下事件:

$scope.$on('$routeUpdate', function(next, current) { 
      $scope.currentPage = $routeParams.page ? Number($routeParams.page) : 1; 

      $scope.search(); 
     }); 

,更新的当前页是在URL后触发调用我的范围search()方法。

我该如何将它转换为使用ui-router的状态? $ routeUpdate不再被触发,因为我使用的是状态管理器而不是路由。

我的路线现在的状态提供者定义为这样:

$stateProvider 
     .state('mandats', { 
      url: '/domiciliations/mandats', 
      templateUrl: 'domiciliations/views/mandats.html', 
      controller: 'mandatsCtrl' 
     }) 
+0

什么是$ stateProvider? – Langdon 2013-04-29 15:45:13

+1

它是AngularJS的ui-router插件的一部分。它提供状态管理。 – Sam 2013-04-29 15:59:53

回答

10

我最终收听了$ locationChangeSuccess事件,而且解决了它。

+0

请注意,'$ locationChangeSuccess'将在您转到其他页面/路由时触发,而'$ routeUpdate'不会。我需要为'$ location.path()'添加额外的检查到处理程序来修复它 – 2017-04-26 13:14:05

0

我会尝试使用controller(mandatsCtrl在你的例子)把那个逻辑。视图控制器会被调用每一个参数的变化,你可以注入$ stateParams到它让页面#

其他类似的选项来探讨的是view controllers这是我一直在某些时候使用

最后,因为$ stateParams是可注入的,所以您可能希望将其注入到您定制的指令并直接使用它