angularJS: ui-router equivalent to $location.search

空扰寡人 提交于 2020-01-04 04:42:07

问题


I'm using the following to do paging in my datagrid:

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

where page is the current page number.

Then I listen to the following event:

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

            $scope.search();
        });

That triggers a call to a search() method on my scope after updating the currentPage that is in the URL.

How would I translate that to states with ui-router ? The $routeUpdate is no longer triggered since I'm using a state manager instead of routes.

My route is now defined in the state provider as such:

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

回答1:


I ended up listening to the $locationChangeSuccess event instead and that solved it.




回答2:


I would try to use the controller (mandatsCtrl in your example) to put that logic. the view controller will be called with every parameter change, and you could inject the $stateParams into it to get the page #

other similar option to explore is the view controllers which is what I've used at some point

last, since $stateParams is injectable, you might want to inject it to your custom-built directive and use it there straight



来源:https://stackoverflow.com/questions/16278951/angularjs-ui-router-equivalent-to-location-search

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!