Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First, your <code>success()</code> handler just returns the data, but that's not returned to the caller of <code>getData()</code> since it's already in a callback. <code>$http</code> is an asynchronous call that returns a <code>$promise</code>, so you have to register a callback for when the data is available.</p> <p>I'd recommend looking up Promises and the <a href="http://docs.angularjs.org/api/ng.$q">$q library</a> in AngularJS since they're the best way to pass around asynchronous calls between services.</p> <p>For simplicity, here's your same code re-written with a function callback provided by the calling controller:</p> <pre><code>var myApp = angular.module('myApp',[]); myApp.service('dataService', function($http) { delete $http.defaults.headers.common['X-Requested-With']; this.getData = function(callbackFunc) { $http({ method: 'GET', url: 'https://www.example.com/api/v1/page', params: 'limit=10, sort_by=created:desc', headers: {'Authorization': 'Token token=xxxxYYYYZzzz'} }).success(function(data){ // With the data succesfully returned, call our callback callbackFunc(data); }).error(function(){ alert("error"); }); } }); myApp.controller('AngularJSCtrl', function($scope, dataService) { $scope.data = null; dataService.getData(function(dataResponse) { $scope.data = dataResponse; }); }); </code></pre> <p>Now, <code>$http</code> actually already returns a $promise, so this can be re-written:</p> <pre><code>var myApp = angular.module('myApp',[]); myApp.service('dataService', function($http) { delete $http.defaults.headers.common['X-Requested-With']; this.getData = function() { // $http() returns a $promise that we can add handlers with .then() return $http({ method: 'GET', url: 'https://www.example.com/api/v1/page', params: 'limit=10, sort_by=created:desc', headers: {'Authorization': 'Token token=xxxxYYYYZzzz'} }); } }); myApp.controller('AngularJSCtrl', function($scope, dataService) { $scope.data = null; dataService.getData().then(function(dataResponse) { $scope.data = dataResponse; }); }); </code></pre> <p>Finally, there's better ways to configure the <code>$http</code> service to handle the headers for you using <code>config()</code> to setup the <code>$httpProvider</code>. Checkout the <a href="http://docs.angularjs.org/api/ng.$http">$http documentation</a> for examples.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload