3

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!

Manu Masson
  • 1,667
  • 3
  • 18
  • 37

2 Answers2

1

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?

Community
  • 1
  • 1
Chris Hermut
  • 1,708
  • 3
  • 17
  • 32
0

To call a certain function every time a controller loads there is this receipt: 1) Define the function on the $rootScope:

$rootScope.myFunction = function(param1, param2){
  //do your stuff
}

and then in every controller just use it like this: myFunction(what, ever);

This way the function will even be called when the controller is reloaded.

Andre Kreienbring
  • 2,457
  • 11
  • 16