Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS call scope function on route change

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.

like image 219
vishesh Avatar asked Sep 15 '26 03:09

vishesh


1 Answers

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();
});
like image 110
Aleksandar Gajic Avatar answered Sep 17 '26 15:09

Aleksandar Gajic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!