$scope.savekbentry = function (value) {
console.log('save clicked');
console.log(value);
console.log($scope.kbentry.kbname);
$scope.kbentry.mode = value;
var kbname = $scope.kbentry.kbname;
var kbdescription = $scope.kbentry.kbname;
var kbmode = "";
var type = "";
if ($scope.kbentry.mode == 'symptom') { kbmode = 1; type = 'SYM' }
if ($scope.kbentry.mode == 'allergy') { kbmode = 3; type = 'ALG' }
$http.post('../AjaxRequestData.aspx/AddKBEntry', { KB_Name: kbname, KB_Des: kbdescription, KB_Mode: kbmode })
.success(function (data, status, headers, config) {
$scope.transport = {
method: 'post',
read: '../WM_Autocomplete/GetAutocompleteData.aspx/GetSingletonLst',
params: { type: type }
}
})
.error(function (data, status, headers, config) {
});
clear();
}
在上面的代码中,我想在第一篇文章成功后调用另一个异步post方法。目前它不能按照上面的代码工作。如何通过回调函数来处理这个问题?
答案 0 :(得分:1)
您可以使用then()
链接并解决$http
请求承诺,例如:
var getSingletonPromise = function(type){
return $http.post('.../GetSingletonLst', type) // returns a promise
};
var addEntryPromise = function(params){
return $http.post('.../AddKBEntry', params) // returns a promise
};
$scope.savekbentry = function (value) {
addEntryPromise().then(function(){ // use then() to resolve your promise
// addEntry onSuccess
}).getSingletonPromise().then(function(){
// addEntry onSuccess
});
}
您可能还会考虑将$ http请求分隔到单独的工厂/服务中并添加错误处理。
请注意,上述代码尚未经过测试,但提供了可能解决方案的概述。