[英]promise is not returning to defer in AngularJs
函数正在执行,但未返回要延迟的userType值。 因此导航到默认页面。
我有两种登录类型,即经理admin,基于UI页面的响应,应导航至相应页面,这是我的要求。 我什至从UI到角度控制器都得到了响应,但在承诺范围内,它没有获取这些值。 任何帮助,不胜感激
angular.module('student').service('StudentRepository',['$http','$q','$rootScope', function(http, $q,rootScope){
var user=[];
this.loginCheck = function(id,password,userType){
var testVar = id + "," +password+","+userType;
var defer = $q.defer();
var studentPromise = http.put('/Stud/login/',testVar);
//in testVar also am getting all these values
studentPromise.then(function(result){
//within this function in result.data am getting null
user = result.data;
alert("the result is"+result.data);
defer.resolve(user);
});
return defer.promise;
};
}]);
angular.module('student').controller('LogonController', ['$http','$scope',
'StudentRepository', '$stateParams', '$state','$rootScope',
function(http,scope,StudentRepository,stateParams,state,rootScope){
alert("we r in logon controller ");
scope.loginCheck = function(){
//enter the input values,after submit
alert("userType"+ scope.user.userType);
StudentRepository.loginCheck(scope.user.id,scope.user.password,scope.user.userType)
.then(function (response){
rootScope.role=response;
alert(" logon controller"+response);
if(response == "manager")
{
scope.manager=response;
alert("The user is manager"+" .... "+scope.manager);
state.go("home");
}
else if(response.data == "admin")
{
scope.admin=response.data;
alert("The user is admin"+" ..... "+scope.admin);
state.go("home");
}
else
{
alert(" ID/Password is Invalid...");
state.go("landingPage");
}
});
};
}
]);
我认为您可以更简单地完成此操作,因为$http.method
已经返回了Promise。
你可以试试这个:
this.loginCheck = function(id,password,userType){
var testVar = id + "," +password+","+userType;
return http.put('/stud/login/',testVar);
};
如果您想处理服务中的成功和错误,也可以这样做:
this.loginCheck = function(id,password,userType){
var testVar = id + "," +password+","+userType;
return http.put('/stud/login/',testVar).then(function (response){
return response // or response.data
}
, function (error){
return error;
});
};
我认为它应该与旅游控制器一起使用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.