3 回答

TA貢獻(xiàn)1873條經(jīng)驗(yàn) 獲得超9個(gè)贊
這是“遞延反模式”嗎?
$http service
promise
這是反模式 app.factory("SomeFactory",['$http','$q']){ return { getData: function(){ var deferred = $q.defer(); $http.get(destinationFactory.url) .then(function (response) { deferred.resolve(response.data); }) .catch(function (error) { deferred.reject(error); }); return deferred.promise; } }}])
app.factory("SomeFactory",['$http']){ return { getData: function(){ //$http itself returns a promise return $http.get(destinationFactory.url); }}
this.var = SomeFactory.getData() .then(function(response) { //some variable = response; },function(response) { //Do error handling here});

TA貢獻(xiàn)1875條經(jīng)驗(yàn) 獲得超3個(gè)贊
$q
$q.when
var deferred = $q.defer();
$q
.
return { getData: function(){ return $http.get(destinationFactory.url) .then(function (response) { if (typeof response.data === 'object') { return response.data; } else { throw new Error('Error message here'); } }); // no need to catch and just re-throw }); }
- 3 回答
- 0 關(guān)注
- 526 瀏覽
添加回答
舉報(bào)