看看我的代碼示例:
我已經剝離下來到(我認爲是)最基本的要素,和我設置得到一個錯誤deferred.reject is not a function
angular
.module('myApp')
.service('myService', MyService);
MyService.$inject = ['$http', '$q'];
function MyService($http, $q) {
var self = this;
self.getResult = getResult;
function getResult(id) {
var deferred = $q.defer();
deferred = $http.get('my/api/method', { params: { id: id } })
.then(getResultCompleted)
.catch(getResultFailed);
function getResultCompleted(response) {
if (response.data.ID > 0) {
deferred.resolve(response.data);
} else {
deferred.reject(response);
}
}
function getResultFailed(response) {
deferred.reject(response);
}
return deferred.promise;
}
}
注:我知道我能得到這個由然而簡單地返回$ HTTP調用的結果,爲了瞭解這些承諾對象而言的工作,我想看看我是否能得到這工作通過宣佈並返回deferred
如上所示
謝謝!
'$ http.get()'返回承諾,但不延遲 – Vadim
@Vadim:我不明白。你可以請Elbourate? – Ricky
我指的是'deferred = $ http.get()'字符串。這是不正確的,因爲'$ http.get()'不會返回延遲對象,但它是承諾的。所以,你可以返回'$ http.get' – Vadim