gpt4 book ai didi

javascript - 如何处理angularjs中的回调?

转载 作者:数据小太阳 更新时间:2023-10-29 04:15:50 24 4
gpt4 key购买 nike

我有一个注册机制,其中 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;
})

最佳答案

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

Controller :

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();
});
};

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

您正在从不需要的服务返回,服务作为实例工作,因此实例已被注入(inject)。是一个需要返回的工厂(单例)。

您将 $scope.registration 设置为与 $rootScope.registration 相同,然后将 $rootScope.registration 设置为与您的 getEnterGeneratedCode 函数中的 $scope.registration 相同,这并不重要,无论如何都应该继承原型(prototype)。

你应该总是尝试像这样定义依赖关系:

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

}]);

除非 $rootScope.success 正在其他地方使用,否则目前设置它毫无意义,我建议避免在 $rootScope 上设置 Prop ,因为它很快就会失控。

这是您的代码的简化版本:

.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
});
};

}]);

关于javascript - 如何处理angularjs中的回调?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27816084/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com