我有一个注册机制,其中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');
}
}
和内部服务
.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;
})
您想$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);
});
服务:
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();
});
};
一些值得注意的事情...
您从不需要的服务返回,该服务作为实例工作,因此该实例已被注入。这是一家需要退货的工厂(单人)。
您要设置$scope.registration
为$rootScope.registration
与您的函数相同,然后再设置$rootScope.registration
为与函数相同,这是不必要的,并且无论如何应原型继承。$scope.registration
getEnterGeneratedCode
您应该始终尝试使用以下方式定义行为:
.controller('RegisterAccountCtrl', ['$scope', '$rootScope', 'registerUser', '$location', function($scope, $rootScope, registerUser, $location){
}]);
除非$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
});
};
}]);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句