AngularJS - 登录后无法正常重定向

时间:2016-01-10 09:58:48

标签: 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;
  }

  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"
    }
  );
})

感谢。

1 个答案:

答案 0 :(得分:1)

您忘记在控制器中注入 $location

试试这个

function MainCtrl(user, auth,$location) {