angularjsng-controller

How to load a controller by default for any page load for a single page app


I am having a single page application, and I want a controller to be loaded only once whenever someone opens my url irrespective of the state.

I have a controller something like this:

app.controller('someController', ['$scope', function($scope){
    $scope.somefunction = function(){
      console.log("Some function is called ");
    }
}])

I want this to be loaded only once when someone opens any of my page.

Currently I tried to add this under my header.html file something like this:

<div class="" ng-controller="someController"></div>

This way I can call this controller for any of the page but the problem is that it will be called everytime if user changes page.

I think there may be some way to add this function under app.js or other some better way to call only once on page load.

Can anyone please help me here.


Solution

  • It sounds like for what you want, you should put the controller on the parent div of the ui-view:

    <div class="container" ng-controller="someController">
        <div ui-view></div>
    </div>