这是我的代码片段,在我的案例中,脚本在获得POST响应之前首先被加载。如何等待POST请求在加载脚本之前完成?
myfunction = function() {
$http.post(Url, requestData).success(
function(response) {
for (i = 0; i < response.result[0].length; i++) { if (response.result[0][i].isActive) {
status.push({
name: response.result[0][i].Name
})
}
}
deferred.resolve(permissions);
},
function(error) {
deferred.reject(error);
}
);
return deferred.promise;
}
}
从我的控制器调用
var status = myfunction();
status.then(function(result) {
console.log("data.name"+ result);
});
// Then do the scripts after POST reququest gets completed.
答案 0 :(得分:2)
为了在post方法完成后执行此操作,您必须在承诺的“then”部分执行此操作:
var status = myfunction();
status.then(function(result) {
console.log("data.name"+ result);
// -----> HERE <-----
// Then do the scripts after POST reququest gets completed.
});