私が知っているように、返す必要があるservice
呼び出しにのみ使用できます。コールバックは、実装したようではなく、コントローラーに実装する必要があります。$http
promise
then
service
何かのようなもの:
myModule.factory('ajax_post', ['$http', function(_http) {
var path = 'src/php/data.ajax.php';
return{
init: function(jsonData){
var _promise= _http.post(path,
jsonData
,{
headers: {
'SOAPActions': 'http://schemas.microsoft.com/sharepoint/soap/UpdateListItems'
}
}
);
return _promise;
}
}
}]);
そして今、コントローラーを呼び出すことができます:
ajax_post.init(jsonData);
そして、コントローラーの約束の「チェーン」は次のようになります。
var promise = asyncFunction(parameters);
promise.then(function (result) {
return otherAsyncFunction(result * 2);
}).then(function(result) {
if (result < 0) { throw "Some Error" };
return mayBeSyncOrAsyncFunction(result);
}).then(function(result) {
console.log("Final Result: " + result);
}, function(error) {
// Handle error (exception, etc).
});
他の良い方法は、次を使用すること$q
です。
複数の約束を待ちたい場合
$q.all([promise, …])
→新しい約束
newPromiseは、指定されたすべての promise が解決されると解決されます。指定された promise のいずれかが拒否された場合、newPromiseも拒否されます。