Run once when AngularJS controller loads

angularjs

Solution

Just do it normally, controllers are just functions injected with $scope, services. When the controller is loaded, this function is called only once. You can do anything inside it.

app.controller("yourController",function($scope, myService){
   //Write your logic here.


   //Initialize your scope as normal
});

Problem

I have some stuff that needs to be done only once when the controller is loaded. What's the best way to this? I've read some about the "run block" but I don't really understand how it works. Some pseudo code: ``` when /app resolove some stuff load a view controllerA ControllerA: Some-magic-piece-of-code-only-run-once{ } ``` Can anyone point me in the right direction?

Original source