我有我想要“重新验证”的数据。所以我需要做一个get请求,保存回调中的数据,删除当前数据,然后用回调中的数据发表帖子。
我需要以某种方式使用$ q。
也许我完全没有,但这是我的尝试。
$scope.reSaveBIM = function(){
var defer = $q.defer();
defer.promise
.then(function(){
$http.get('/api/bim/' + $scope.activePartOfBim._id)
.success(function(fullBIM){
console.log(fullBIM); //Defined
return fullBIM;
}
);
})
.then(function(fullBIM){
console.log(fullBIM); //Undefined
$http.delete('/api/bim/' + $scope.activePartOfBim._id);
return fullBIM
})
.then(function(fullBIM){
$http.post('/api/bim', {bim:JSON.stringify(fullBIM)});
});
defer.resolve()
};
来自第一个回调的数据不会在链接中返回。我在正确的轨道上吗?我也尝试使用$ q.all,但失败了。
任何想法?
当然,我可以建一个金字塔,但我想学习用承诺来平息它。 – Per 2014-09-30 16:54:18
$ http已经对promises进行了操作。 .success()等同于.then()。如果你想平整你的代码,那么你不应该内联你的响应函数。尝试将函数作为参数传递给成功方法,即.success(mySuccessFunction); – 2014-09-30 16:58:48
存在一个微妙的问题:'firstSuccessFunction'在传递给'.success(firstSuccessFunction)'时是'undefined'。 – 2014-09-30 19:22:04