AngularJS-登录后重定向不工作
用户成功登录后,我希望页面重定向到主页。我的代码当前看起来像:AngularJS-登录后重定向不工作,angularjs,Angularjs,用户成功登录后,我希望页面重定向到主页。我的代码当前看起来像: function MainCtrl(user, auth) { var self = this; function handleRequest(res) { var token = res.data ? res.data.token : null; if(token) { $location.path('/'); } self.message = res.data.message; } s
function MainCtrl(user, auth) {
var self = this;
function handleRequest(res) {
var token = res.data ? res.data.token : null;
if(token) { $location.path('/'); }
self.message = res.data.message;
}
self.login = function() {
user.login(self.username, self.password)
.then(handleRequest, handleRequest)
}
}
为什么行$location.path('/')代码>不工作,我应该在那里进行重定向吗
以及路由配置部分:
.config(function($routeProvider){
$routeProvider.when("/",
{
templateUrl: "views/home.html"
}
)
.when("/login",
{
templateUrl: "views/login.html"
}
)
.when("/register",
{
templateUrl: "views/register.html"
}
);
})
谢谢。您忘记在控制器中注入
像这样试试
function MainCtrl(user, auth,$location) {
请提供您的路线配置部分,登录后要打开哪个页面?家,不是吗?是的,是的,家