如何处理angularjs中的回调?

Has*_*aan 8 javascript callback angularjs

我有一个注册机制,其中rootscope变量通过服务发送.成功后,它会更新该$rootScope.success字段.但angularjs服务依赖于回调.服务更新了rootscope.success,但函数顺序执行代码.

我如何等待服务完成其响应然后进一步处理?

.controller('RegisterAccountCtrl', function ($scope,$rootScope,registerUser,$location) {

    $rootScope.success = false;
    $scope.registration = $rootScope.registration;

$scope.getEnterGeneratedCode = function(){
        $rootScope.registration = $scope.registration;
        registerUser.registerUser();
        if($rootScope.success){
           $location.path('/confirm');
        }
}
Run Code Online (Sandbox Code Playgroud)

内部服务

.service('registerUser',function($http,$rootScope,$ionicLoading){
    this.registerUser = function(){
        $ionicLoading.show();
        $http({
            method: 'POST',
            datatype:'json',
            data:{obj:$rootScope.registration},
            url: 'http://localhost/LoginService.asmx/CreateUser',
            contentType: "application/json; charset=utf-8",
            cache: false
        }).success(function (data, status, headers, config){
            if (status == '200') {
                var obj = data;
                $rootScope.success = true;
                $ionicLoading.hide();
                //alert(obj);
            }
        }).error(function (data, status, headers, config){
            $ionicLoading.hide();
        });
    };

 return this;
})
Run Code Online (Sandbox Code Playgroud)

Sim*_*ton 6

您想$http从registerUser 返回您的请求,然后您可以在服务器中使用它,就像在服务中使用它一样.

控制器:

registerUser.registerUser().success(function(data, status, headers, config){
   //This code will now execute when the $http request has resolved with a success
   if($rootScope.success){
      $location.path('/confirm');
   }
}).error(function(error, status, headers, config){
   //An error occurred in $http request
   console.log(error); 
});
Run Code Online (Sandbox Code Playgroud)

服务:

this.registerUser = function(){
    $ionicLoading.show();
    return $http({
        method: 'POST',
        datatype:'json',
        data:{obj:$rootScope.registration},
        url: 'http://localhost/LoginService.asmx/CreateUser',
        contentType: "application/json; charset=utf-8",
        cache: false
    }).success(function (data, status, headers, config){
        if (status == '200') {
            var obj = data;
            $rootScope.success = true;
            $ionicLoading.hide();
            //alert(obj);
        }
    }).error(function (data, status, headers, config){
        $ionicLoading.hide();
    });
};
Run Code Online (Sandbox Code Playgroud)

一些值得注意的事情......

您从不需要的服务返回,服务作为实例工作,因此实例已经注入.这是一个需要返回的工厂(单身人士).

您将设置$scope.registration为相同$rootScope.registration,然后设置$rootScope.registration$scope.registration与您的getEnterGeneratedCode函数中的相同,这是不合理的,并且应该原型继承.

你应该总是尝试用这样定义depedencys:

.controller('RegisterAccountCtrl', ['$scope', '$rootScope', 'registerUser', '$location', function($scope, $rootScope, registerUser, $location){

}]);
Run Code Online (Sandbox Code Playgroud)

除非$rootScope.success在别处使用,否则它目前无意义设置,我建议避免在你的$ rootScope上设置道具,因为它很快就会失控.

以下是代码的简化版本:

.controller('RegisterAccountCtrl', [
    '$scope',
    '$rootScope',
    'registerUser',
    '$location',
function($scope, $rootScope, registerUser, $location) {

    $scope.getEnterGeneratedCode = function() {
        $ionicLoading.show();
        registerUser.registerUser().success(function(data, status, headers, config) {
            if (status == '200') {
                var obj = data;
                $ionicLoading.hide();
                $location.path('/confirm');
                //alert(obj);
            }
        }).error(function(data, status, headers, config) {
            $ionicLoading.hide();
        });

    }

}])

.service('registerUser', [
    '$http',
    '$rootScope',
    '$ionicLoading',
function($http, $rootScope, $ionicLoading) {

    this.registerUser = function() {
        return $http({
            method: 'POST',
            datatype: 'json',
            data: {
                obj: $rootScope.registration
            },
            url: 'http://localhost/LoginService.asmx/CreateUser',
            contentType: "application/json; charset=utf-8",
            cache: false
        });
    };

}]);
Run Code Online (Sandbox Code Playgroud)