title: AngularJS - How to pass data to a controller
body: <p>I'm having a hard time passing data to a controller. My scenario is that I have a function that gets a json object and I need to pass that object to a controller that does the rest of the work. Below is what I'm working with:</p>
  
  <p>Getting the data before DOM loads and save it in the <code>Data</code> variable.</p>
  
  <pre><code>    $.ajax({
      url: "/_api/web/lists/getbytitle('Consultant%20Profile')/items?$filter=ID%20eq%20"+curItemId,
      type: "GET",
      headers: {"Accept": "application/json;odata=verbose"},
      success: function(data){
          Data = data.d.results; //global variable that I want to pass.
          console.log(Data);
  
      },
      error: function(data){
          console.log("something is not right with - ", data)
      }   
     });
  </code></pre>
  
  <p>This is the controller that I would like to pass <code>Data</code> to.</p>
  
  <pre><code>var app = angular.module("contactApp",[]);
  app.value('listData', Data);
  app.controller('ContactController',['listData','$scope','$http',function(listData,$scope,$http){
          console.log(listData);
          //Do something here...  
  });
  </code></pre>
  
  <p>I'm trying to use <code>value()</code> but getting an error if I pass in anything other than a string. So if <code>Data = "Sometext"</code> it would console out that text but if it's an object it doesn't work. Not sure what I'm doing wrong but is there a better way of doing this!?</p>