javascript变量的异步函数和范围问题

javascript变量的异步函数和范围问题,javascript,angularjs,angular-promise,Javascript,Angularjs,Angular Promise,我有以下调用REST Web服务的代码。web服务返回一个布尔值 checkCurrentPassword: function (passwordInfo) { var valid = false; $http.post('/api/preference/password/check', passwordInfo) .success(function (data) { valid = data;

我有以下调用REST Web服务的代码。web服务返回一个
布尔值

    checkCurrentPassword: function (passwordInfo) {
        var valid = false;
        $http.post('/api/preference/password/check', passwordInfo)
            .success(function (data) {
                valid = data;
            });
        return valid;
    }
问题在于,当
checkCurrentPassword
返回
valid
时,仍然是
false
,因为ajax调用由于其异步性质尚未完成

我不知道如何确保
有效
变量正确分配了从服务器返回的值

有人能帮忙吗

编辑1

使用回调,似乎我只是将问题转移到控制器层。。。见下文:

角度服务:

checkCurrentPassword: function (passwordInfo, callback) {
              return  $http.post('/api/preference/password/check', passwordInfo)
                    .success(function (data) {
                        callback(data);
                    });
            }
 $scope.updatePassword = function () {
            if ($scope.updatePasswordForm.$valid) {

                var valid = false;

                passwordService.checkCurrentPassword({plainPassword: $scope.passwordInfo.currentPassword}, function(data, status){
                    valid = data;
                });

                console.log(valid);//always false

                passwordService.updatePassword($scope.passwordInfo, function (data, status) {
                    console.log('Updated password successfully');
                    $state.go('dashboard');
                });
            }
        };
来自角度控制器:

checkCurrentPassword: function (passwordInfo, callback) {
              return  $http.post('/api/preference/password/check', passwordInfo)
                    .success(function (data) {
                        callback(data);
                    });
            }
 $scope.updatePassword = function () {
            if ($scope.updatePasswordForm.$valid) {

                var valid = false;

                passwordService.checkCurrentPassword({plainPassword: $scope.passwordInfo.currentPassword}, function(data, status){
                    valid = data;
                });

                console.log(valid);//always false

                passwordService.updatePassword($scope.passwordInfo, function (data, status) {
                    console.log('Updated password successfully');
                    $state.go('dashboard');
                });
            }
        };
编辑2:以下是我如何重写控制器:

$scope.updatePassword = function () {
            if ($scope.updatePasswordForm.$valid) {
                passwordService.checkCurrentPassword({plainPassword: $scope.passwordInfo.currentPassword}).success(function (data) {
                    if (data) {
                        passwordService.updatePassword($scope.passwordInfo, function (data, status) {
                            console.log('Updated password successfully');
                            $state.go('dashboard');
                        });
                    }
                });
            }
        };

您应该返回由
$http.get
返回的承诺(HttpPromise),请参阅:$q和$http

此示例可能会帮助您:

angular.module('app',[]);

angular.module('app').service('Foo',function($q){

  this.checkPassword = function(value){
    //replace this example code with a $http call
    console.log('checkPassword:',value);
    var deferred = $q.defer();
    setTimeout(function(){
      if(value && value.length >= 4){
        deferred.resolve(value);        
      } else {
        deferred.reject('oops invalid password!!!');
      }   
    },100);
    return deferred.promise;
  };

  this.updatePassword = function(value){
    //replace this example code with a $http call
    console.log('updatePassword:',value);
    var deferred = $q.defer();
    setTimeout(function(){
      if(Math.random() > 0.5){
        deferred.resolve('password updated');        
      } else {
        deferred.reject('error while updating password');
      }   
    },100);
    return deferred.promise;
  };

});

angular.module('app').run(function(Foo){

  Foo.checkPassword('tolkjlkj')
    .then(Foo.updatePassword)
    .then(function(result){
      console.log('Yes',result);
    }).catch(function(error){
      console.log('Oops',error);
    });

});

我想你可以通过回调来解决这个问题

checkCurrentPassword: function (passwordInfo) {

  $http.post('/api/preference/password/check', passwordInfo)
             .success(function (data) {
                 return data;
             }).error(function (data) {
                 return false;
             });

}
你应该使用

你的函数看起来像这样。 (确保在顶部代码中插入$q)


})

使用回调:
.success(函数(数据){myCallback(数据);})看看谢谢大家。我已经相应地编辑了我的文章。问题是我需要一个来自调用方/客户端的纯布尔值。@balteo-没有办法做到这一点。不能强制异步操作阻塞并等待。您必须使用一个承诺,或者使用一个回调。看起来您是从内部函数返回数据。我需要从外部函数返回它。。。