如何将响应值传递给父对象。在 angularjs 中进行 http 服务调用的调用者?我拥有的是一个 BaseModel ,它将执行如下操作。这个想法是 basemodel 对象实例应该具有响应值。顺便说一句,我试图避免使用广播等。
调用对象
model = new BaseModel(); model.get();
定义:
BaseModel.$service = ['$http', '$q', function ($http, $q) { return function () { return new BaseModel($http, $q); };
}];
实际的BaseModel:
function BaseModel($http, $q) { var q = $q.defer(); this.http = $http; this.response = null // this is to hold the response value this.get = function () { var request = this.http({ url: 'http://blahblah.com?a=1&b=2', method: "GET", }); request.success(function (response) { q.resolve(response); }); q.promise.then( function(response){ console.log(response, ' got response'); //the idea is to have this.response = response return response; } ); return q.promise };