1

After a user successfully logs in i would like the page to redirect to the homepage. My code currently looks like:

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

}

Why does the line $location.path('/'); not work, is that where i should actually have the redirect?

And the route config part:

.config(function($routeProvider){
  $routeProvider.when("/",
    {
      templateUrl: "views/home.html"
    }
  )
  .when("/login",
    {
      templateUrl: "views/login.html"
    }
  )
  .when("/register",
    {
      templateUrl: "views/register.html"
    }
  );
})

Thanks.

3
  • Please provide your route config part Jan 10, 2016 at 10:03
  • which page you want to open after login? home isn't it? Jan 10, 2016 at 10:33
  • Yes thats right, home
    – userMod2
    Jan 10, 2016 at 10:35

1 Answer 1

1

You forget to inject $location in your controller

Try like this

function MainCtrl(user, auth,$location) {
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.