我有一个登录页面,如果用户登录,我想将用户重定向到另一个
HTML页面,在那里我将列出我从服务器获得的用户任务.
问题是:
即使我写的函数正常工作,后端API返回我想要的值(我可以看到控制台上的值详细信息)当我使用重定向代码$window.location.href =’../ Kullanici/userPanel.html页面重定向登录后立即,由于某种原因,我不能使用重定向后的函数返回的值.不仅如此,我再也看不到控制台日志中返回的值的详细信息.
这是我的代码:
控制器:
app.controller('myCtrl',['$scope','$http','$window','$mdToast','userTaskList',function ($scope,$http,$window,$mdToast,userTaskList) { $scope.siteLogin = function () { var userName = $scope.panel.loginUserName; var password = $scope.panel.loginPassword; var loginMember = { //JSON data from login form K_ADI: $scope.panel.loginUserName,PAROLA: $scope.panel.loginPassword }; $http({ method: 'POST',url: 'http://localhost:5169/api/Kullanicilar/KullaniciDogrula',headers: { 'Content-Type': 'application/json' },data: loginMember }).then(function successCallback(response) { console.log("message sent",response); $scope.data = response.data.error.data; if ($scope.data === true) {//if username and password is correct console.log("User exists"); userTaskList.showActiveTasks(userName) .then(function (activeTaskResponse) { var activeTasks = activeTaskResponse; console.log("Active tasks (controller): ",activeTaskResponse); userTaskList.showFinishedTasks(userName) .then(function (finishedTaskResponse) { var finishedTasks = finishedTaskResponse; console.log("Finished tasks(controller): ",finishedTaskResponse); $scope.getMessage(); $window.location.href = '../Kullanici/userPanel.html'; },function (err) { console.log(err); }); },function (err) { console.log(err); }); } },function errorCallback(response) { console.log("Couldn't send",response); }); }
那么是什么导致了这个问题,我该如何解决?
编辑:我嵌套.然后部分,但它不能正常工作,并给出这个值现在只是评估现在警告.所以我不能在重定向的HTML页面上使用数据.
解决方法
我会在第一个promise中嵌套你的两个函数,然后在完成所有这些函数后重定向.就像是
app.controller('myCtrl',userTaskList) { $scope.siteLogin = function () { var userName = $scope.panel.loginUserName; var password = $scope.panel.loginPassword; var loginMember = { //JSON data from login form K_ADI: $scope.panel.loginUserName,PAROLA: $scope.panel.loginPassword }; $http({ method: 'POST',headers: { 'Content-Type': 'application/json' },data: loginMember }).then(function successCallback(response) { console.log("message sent",response); $scope.data = response.data.error.data; if ($scope.data === true) {//if username and password is correct console.log("User exists"); userTaskList.showActiveTasks(userName) .then(function (res) { var activeTasks = res; console.log("Active tasks (controller): ",res); userTaskList.showFinishedTasks(userName) .then(function (res) { var finishedTasks = res; console.log("Finished tasks(controller): ",res); $scope.getMessage(); $window.location.href = '../Kullanici/userPanel.html'; },function (err) { console.log(err); }); },function (err) { console.log(err); }); } else { //if username or password is wrong $mdToast.show( $mdToast.simple() .textContent('Username or Password is wrong') .position('right') .hideDelay(3000) ); } },function errorCallback(response) { console.log("Couldn't send",response); }); } } ]);