私は工場からgetBookIDsを呼び出し、その結果を使って同じファクトリからgetBookInfoを呼び出します。しかし、Console.log(bookInfo)には前の呼び出しの結果が表示されます! 返す前にdeferred.promise値を更新するにはどうすればよいですか?update q返す前に約束を延期する
これは私のコントローラ
であり、これはすべての角度のサービスはシングルトンなので、私はあなたがこのバグを得た理由はgetBookIDs
とgetBookList
同じdeferred
を共有していると思います私の工場
app = angular.module('myApp');
app.factory("MainFactory", ['$soap', '$http', '$q', function ($soap, $http, $q) {
var viewFactory = {};
var deferred = $q.defer();
viewFactory.getBookIDs = function() {
//var bookIDs = [];
$http({
url: 'http://127.0.0.1/client.php?fn=getBooks',
method: "GET"
}).then(function success(response) {
deferred.resolve(response.data.result);
}, function myError(error) {
console.log('error', error);
});
return deferred.promise;
};
viewFactory.getBookList = function (bookIDs) {
$http({
url: 'http://127.0.0.1/client.php?fn=getBooksInfo&p1=' + bookIDs,
method: "GET"
}).then(function success(response) {
deferred.resolve(response.data.result);
}, function myError(error) {
deferred.reject(error);
});
return deferred.promise;
};
return viewFactory;
}]);
それぞれの方法で新しい約束を返すべきです。 –