2

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.

2 Answers 2

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();
});
Sign up to request clarification or add additional context in comments.

1 Comment

I previously had '$stateChangeStart' and after changing to 'routeChangeStart' it worked! thank you!
7

You can try this in your controller :

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.