在使用角度路由的单页面应用程序中,如何在 api 调用后重定向页面。就我而言,我想在用户调用登录 api 后将用户重定向到个人资料页面。所以这就是我认为可行的方法,但事实并非如此。
在客户端,main.js。我已经设置了角度路由
app.config(function($routeProvider){ $routeProvider
//the home page display
.when('/', {
templateUrl: 'main.html',
controller: 'mainController'
})
.when('/login', {
templateUrl: 'login.html',
controller: 'loginController'
})
.when('/signUp', {
templateUrl: 'signUp.html',
controller: 'signUpController'
})
.when('/profile', {
templateUrl: 'profile.html',
//controller: 'mainController'
}); });
我从我的控制器调用 /login post api
app.controller('authController', function($scope, $http, $rootScope, $location){
$scope.user = {username: '', password: ''};
$scope.error_message = '';
$scope.login = function(){
$http.post('/login', $scope.user).success(function(data){
if(data.state == 'success'){
//set username authenticated property to true after successful log in
//I am only pasting some of my code here, more logic before controller
$rootScope.authenticated = true;
$rootScope.current_user = "james";
$location.path('/profile');
}
else{
$scope.error_message = data.message;
}
});
};
});
这是我的登录 api
router.post('/login', passport.authenticate('local-login', {
successRedirect : '/success', // redirect to the secure profile section
failureRedirect : '/failure', // redirect back to the signup page if there is an error
failureFlash : true // allow flash messages
}));
当成功时,它会调用 success,它会发回数据,该数据应触发 $http.post 中的回调,并通过 $location.path('/profile'); 重定向页面。但是,回调未被调用,我的页面显示来自 res.send 的用户信息
//sends successful login state back to angular
router.get('/success', function(req, res){
res.send({state: 'success', user: req.user ? req.user : null});
});
我走在正确的轨道上吗?我只是按照微软的教程https://www.microsoftvirtualacademy.com/en-us/training-courses/mean-stack-jump-start-8442 https://www.microsoftvirtualacademy.com/en-us/training-courses/mean-stack-jump-start-8442但他们在 github 上完成的页面甚至无法工作,所以它不能帮助我调试我的这个问题。