Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call a function on every template/controller onload

I just a made a function for my app that checks the users info. I created this in my authentication controller in the $rootScope

My problem is that now, I have to manually call that function in all the controllers that I have.

There must be a way to automatically call that on all pages when the page loads.

Thanks!

like image 448
Manu Masson Avatar asked Sep 27 '26 02:09

Manu Masson


1 Answers

Have a look at $location service.

After url changes it broadcasts '$locationChangeSuccess' so from there you can just use $scope.$on();

Ref. https://docs.angularjs.org/api/ng/service/$location

Update: After reading your comment you might be better of using .run

AngularJS app.run() documentation?

like image 119
Chris Hermut Avatar answered Sep 28 '26 21:09

Chris Hermut



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!