最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - AngularJS call scope function on route change - Stack Overflow

programmeradmin3浏览0评论

I am using ng-route in in my angular app. As soon as the url changes I don't want to update the templateUrl or controller, but I want to call a function in scope with route param. How to do it?

Should I provide same controller and call the function in controller itself? This is assuming that the controller's constructor is called again on route change. I haven't tried it as there might be a better way to do it? I am new to angular.

I am using ng-route in in my angular app. As soon as the url changes I don't want to update the templateUrl or controller, but I want to call a function in scope with route param. How to do it?

Should I provide same controller and call the function in controller itself? This is assuming that the controller's constructor is called again on route change. I haven't tried it as there might be a better way to do it? I am new to angular.

Share Improve this question asked Jun 18, 2015 at 14:02 visheshvishesh 2,0457 gold badges34 silver badges68 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 8

angular-route.js broadcast events $routeUpdate, $routeChangeStart, $routeChangeSuccess on $rootScope, so you can listen to one of them

add this to your scope on which you want to detect route change

$rootScope.$on('$routeChangeStart', function (next, last) {
   $scope.someFunction();
});

You can try this in your controller :

$scope.$on('$locationChangeStart', function(event) {
    //your code here
});
发布评论

评论列表(0)

  1. 暂无评论