且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

AngularJS $ http结果与承诺

更新时间:2023-08-31 11:33:34

在这种情况下,您不要使用$q,因为$http已经返回了诺言.将2一起使用效率低下. (如果您使用的是非角度异步功能,例如地理查询,则使用$q.

You should not use $q in this instance, as $http already returns a promise. Using to 2 together in inefficient. ($q is of use if you are using a non-angular async function, such as a Geo lookup).

Services.js:

Services.js :

.service('httpService', function($http, $timeout) {

  var asyncRequest = function(url) {
    return $http.get(url)
  };
  return {
   asyncRequest : asyncRequest 
  };

});

Controller.js:

Controller.js :

var result = httpService.asyncRequest(url)
.then(function(res) {
    console.log(res.data);
}, function(error) {
    alert("Error...!");
});