成功后在函数A中调用函数A 在angularjs中调用函数B
call function A in function A after success call function B in angular js
I am using some functions that send $http async request to server side.i want to call same current function after relogin user if response is "notlogin". I think i need to use promise but how?
$scope.A = function(){
$http({..,async: "isAsync",...})
.success(function (response) {
if (response.d == "notlogin") {
if ($scope.B())//call back login to refresh session
$scope.A();//repeat request if login return true
}
});
};
$scope.B= function () {
$scope.userlogin_error = "wait...";
$http({...,async: "isAsync",...}).success(function (response) {
if (response.d == "True") {
$scope.userlogin_error = "login success";
$scope.user_islogin = true;
return true;
}
});
}
尝试将 $scope.B() 设为 promise(这是异步的,我理解正确吗?);
$scope.B = function(){
var defer = $q.defer();
if(something) {
defer.resolve(data);
} else {
defer.reject(error);
}
return defer.promise;
};
$scope.B().then(function(data){
$scope.A();
});
您可以 return 通过 return 调用 $http 调用
成功回调 $scope.B()
$scope.A = function(){
$http({..,async: "isAsync",...}).success(function (response) {
if (response.d == "notlogin") {
$scope.B().success(function(data){
if(data.d == "True"){
$scope.userlogin_error = "login success";
$scope.user_islogin = true;
$scope.A();
}
});
}
});
};
$scope.B= function () {
return $http({...,async: "isAsync",...});
};
I am using some functions that send $http async request to server side.i want to call same current function after relogin user if response is "notlogin". I think i need to use promise but how?
$scope.A = function(){
$http({..,async: "isAsync",...})
.success(function (response) {
if (response.d == "notlogin") {
if ($scope.B())//call back login to refresh session
$scope.A();//repeat request if login return true
}
});
};
$scope.B= function () {
$scope.userlogin_error = "wait...";
$http({...,async: "isAsync",...}).success(function (response) {
if (response.d == "True") {
$scope.userlogin_error = "login success";
$scope.user_islogin = true;
return true;
}
});
}
尝试将 $scope.B() 设为 promise(这是异步的,我理解正确吗?);
$scope.B = function(){
var defer = $q.defer();
if(something) {
defer.resolve(data);
} else {
defer.reject(error);
}
return defer.promise;
};
$scope.B().then(function(data){
$scope.A();
});
您可以 return 通过 return 调用 $http 调用
成功回调 $scope.B()$scope.A = function(){
$http({..,async: "isAsync",...}).success(function (response) {
if (response.d == "notlogin") {
$scope.B().success(function(data){
if(data.d == "True"){
$scope.userlogin_error = "login success";
$scope.user_islogin = true;
$scope.A();
}
});
}
});
};
$scope.B= function () {
return $http({...,async: "isAsync",...});
};