简体   繁体   中英

How to return AngularJS Function's $http response?

I am working on some AngularJS examples. When I click a href link getTestUserName() is triggered and it is return undefined. I just want to see response data from $http. Purpose of this code block that I wrote is avoiding code repeating.

DashboardController.js

ajs.controller("DashboardController", function($scope, $http, $localStorage, $window){
  var token_header = "Token " + $scope.token;
  $scope.getTestUserName = function (target_url) {
    $http({
      url: target_url,
      method: "GET",
      async: false,
      crossDomain: true,
      processData: false,
      contentType: false,
      headers: {
        "Authorization": token_header
      }
    }).success(function onSuccess(response, status) {          
        return response;
    }).catch(function onError(response) {
      //to do
    });
  };  

  $scope.loadTest=function () {
    console.log($scope.getTestUserName("http://0.0.0.0:8001/user/users/1/"));
  };

});

app.js

var ajs = angular.module('my-app', ['ngRoute','ngStorage']);

dashboard.html

<a href="javascript:void(0);" ng-click="loadTest()">Test Click</a>

If I write the $timeout function in loadTest() function, it's working. But I think it's not a good solution to handle it. So, How do I get response from getTestUserName() function.

you can archive your goal in two ways ... or RETURN the $http call from your getTestUserName() method like so:

 $scope.getTestUserName = function (target_url) {
   return $http({
      url: target_url,
      method: "GET",
      async: false,
      crossDomain: true,
      processData: false,
      contentType: false,
      headers: {
        "Authorization": token_header
      }
    });
  };  

OR PILOT 'MANUALLY' by yourself the promise ..with $q like:

 $scope.getTestUserName = function (target_url) {
var deferred= $q.defer(); //<--declare it and initilize

    $http({
      url: target_url,
      method: "GET",
      async: false,
      crossDomain: true,
      processData: false,
      contentType: false,
      headers: {
        "Authorization": token_header
      }
    }).success(function (response) {          
        deferred.resolve(response); //MARK AS REVOLVED OK
    }).catch(function (err) {
      deferred.reject(rerr); //MARK AS REVOLVED WITH ERROR
    });

return deferred.promise; //<--here you return it
  }; 

and so you can call it as :

$scope.loadTest=function () {


$scope.getTestUserName("http://0.0.0.0:8001/user/users/1/").then(function(resp){ 
console.log(resp);
}).catch(function(err){ 
console.log(err);
}));
  };

Hope it helps you!..

Create a Service

function find ()
  {

    var deferred = $q.defer();

    $http.get('url')
      .success(function(data){
        deferred.resolve(data);
      })
      .error(function(error){
        deferred.reject(error.message);
      });

      return deferred.promise;

  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM