不执行从我的角度控制器我正在做一个函数调用到$ http.post()请求。
$scope.formSuccess = false;
$scope.submit = function(serviceName) {
submitRequest(serviceName);
if($scope.formSuccess) {
// do something
}
};
var submitRequest = function(serviceName) {
$http.post(serviceName, data, { headers: { 'Content-type': 'application/json' }
}).then(successFunction, errorFunction);
};
var successFunction = function(response) {
$scope.formSuccess =true;
};
submitRequest()之下的代码首先执行的是successFunction(),所以if块内的代码永远不会执行。我怎样才能做到这一点。我不想移动,如果阻止successFunction。
这是行不通的执行。你的服务被调用,然后如果formSuccess运行,并且将始终返回false,因为你的http请求永远不会在if语句之前完成。请阅读关于http的文档https://docs.angularjs.org/api/ng/service/$http另外一个更好的例子https://thinkster.io/http – Ronnie