title: Sharing a variable between controllers in angular.js
body: <p>I'm new to angular and I'm wondering how I can share a variable between controllers in angular. I'm using the following scripts - </p>
  
  <p>In Main.js:</p>
  
  <pre><code>function MainCntl($scope) {
    ---code
  }
  
  function SearchCtrl($scope, $http) {
      $scope.url = 'http://10.0.0.13:9000/processAdHoc';
      $scope.errorM = "No results";     
      $scope.search = function() {
  
          $http.post($scope.url, { "data" : $scope.keywords}).
          success(function(data, status) {
              $scope.status = status;
              $scope.data = data;
              $scope.result = data; 
              alert('yes');
          })
          .
          error(function(data, status) {
              $scope.data = data || "Request failed";
              $scope.status = status;   
              alert('no');
              $scope.result = "failed";
          });
      };
  }
  </code></pre>
  
  <p>In Index.html </p>
  
  <pre><code>&lt;body ng-controller="MainCntl" &gt;
  ---code
  &lt;div ng-controller="SearchCtrl"&gt;
       &lt;form class="well form-search"&gt;
       &lt;div class="ui-widget"&gt;
            &lt;label for="tags"&gt;&lt;/label&gt;
            &lt;a ng-click="search()"&gt;&lt;input type="image" src="../../images/search1.png" class="searchbox_submit" /&gt;&lt;/a&gt;
            &lt;input ng-model="keywords" placeholder="Shadow Search" id="tags" class="input-medium search-query rounded" /&gt; 
       &lt;/div&gt;
       &lt;/form&gt;
  &lt;/div&gt;
  ---code
  &lt;p ng-model="result"&gt;
       {{result}}
  &lt;/p&gt;
  &lt;/body&gt;
  </code></pre>
  
  <p>Everything works well with the ajax I'm sending data and receiving a response, my question is as follows: </p>
  
  <p>In the SearchCtrl function I have a variable called $scope.result that is later referred to in Index.html. If I insert the html code that contains that variable into the SearchCtrl controller it works fine but if it is in the MainCtrl controller it does not work. How can I share this variable between the controllers.</p>
  
  <p>Thanks ahead </p>