title: Share Variable between controllers of different routes - AngularJS
body: <p>I'm new on Angularjs and I'm trying to build my first application. Let's say I have to routes that loads two different views:</p>
  
  <ul>
  <li>127.0.0.1:8080/site</li>
  <li>127.0.0.1:8080/site_details</li>
  </ul>
  
  <p>Maybe having two different routes is not the right procedure but that it is another problem.</p>
  
  <p>I have two controllers:
   <strong>Controller 1:</strong></p>
  
  <pre><code>app.controller('controller_1', function($scope, $http, user) {
            user.set('Test Example')
  });
  </code></pre>
  
  <p>and  <strong>Controller 2</strong></p>
  
  <pre><code>app.controller('controller_2', function($scope, $http, user) {
          var xxx = user.get()
  });
  </code></pre>
  
  <p>What I want to do is to share data between these two controllers. To do that I did a service in this way:</p>
  
  <pre><code>app.factory('user', function($rootScope) {
   var savedData = {}
  
   function set(data) {
     savedData = data;
   }
   function get() {
    return savedData;
   }
  
   return {
    set: set,
    get: get
   }
  
  });
  </code></pre>
  
  <p>By looking around it seems that having a service built like this should solve the problem. However, what I obtain with the function get() in controller 2 is always an empty return. 
  By setting breakpoints I can see that both set() and get() functions enters in their respective function in the service. </p>
  
  <p>Is this a correct procedure to share data between controllers belonging of different routes?</p>
  
  <p><strong>EDIT1</strong>
  The two views are  built in the same ways and the are loaded inside ng-view</p>
  
  <pre><code>&lt;html ng-app="app" ng-controller='controller_1'&gt;
          CONTROLLER 1
  &lt;/html&gt;
  </code></pre>