title: AngularJS : Keep view state between differents views with differents controllers
body: <p>My angular application starts on a /home state ( i use ui-router ), which have its own controller, in which i call a factory method to get some json objects from my API that i share thanks to this factory between the whole app. I put the list that i get from this in a $scope object of the /home state.</p>
  
  <p>What i want to do is to call this init method of the factory only once, even better would be that the /home controller is only called once.</p>
  
  <p>Actually when i go to another state like /about, and when i go back to /home, the /home controller is called again and so is the init method of the factory. So how do i keep the initial view state ?</p>
  
  <p><strong>StuffFactory.js :</strong></p>
  
  <pre><code>app.factory('StuffFactory', function ($window, $http, $q) {
  
  var stuffs;
  
  return {
      initStuffsList: function () {
  
          return $http.get('http://myapi/stuff/init')
                  .then(function (response) {
  
                      stuffs = (angular.fromJson(response.data.result));
  
                  });
  
  
      },
      getStuffs: function () {
          return stuffs;
      }
  
  };
  </code></pre>
  
  <p>});</p>
  
  <p><strong>homeController.js :</strong> </p>
  
  <pre><code>app.controller('homeCtrl', function ($scope,StuffFactory) {
  
  
  StuffFactory.initStuffsList().then(function(){
           $scope.stuffs = StuffFactory.getStuffs();
  
         });
  
  });
  </code></pre>
  
  <p><strong>app.js :</strong></p>
  
  <pre><code>        .config(['$stateProvider', '$urlRouterProvider']
  
  
              $urlRouterProvider.otherwise('/home');
  
              $stateProvider
  
  
                      .state('home', {
                          url: '/home',
                          templateUrl: './partials/home.html',
                          controller:homeCtrl
                          })
                          .state('about', {
                              url: '/about',
                              templateUrl: './partials/about.html',
                              controller:aboutCtrl
                          })
  </code></pre>