情况就是这样:
$scope.signupNow = function () {
retrieveData();
console.log($scope.userid);
}
function retrieveData(){
// ** some http request **
$scope.userid = id;
}
现在http请求需要一些时间来执行,“$ scope.userid”中没有值,因为它只是在调用函数后打印。
你能否建议如何等待回应????
答案 0 :(得分:3)
$scope.signupNow = function () {
retrieveData().then(funtion(response) {
console.log(response.data);
// do here what you need
});
}
function retrieveData(){
return $http.get('url');
}
答案 1 :(得分:0)
好的,如果你想等到http响应,那么你可以使用回调函数
$scope.signupNow = function () {
retrieveData(function(){
console.log($scope.userid);
});
}
function retrieveData(callback){
// ** some http request **
$scope.userid = id;
callback();
}
答案 2 :(得分:0)
使用angularJs promise -
$scope.signupNow = function () {
var promise = retrieveData();
promise.then(function(success) {
console.log($scope.userid);
}, function(reason) {
alert('Failed: ' + reason);
});
}
function retrieveData(){
var deferred = $q.defer();
(your http request).then(){
$scope.userid = id;
deferred.resolve('success');
}
return deferred .promise;
}