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.

userMod2
  • 8,312
  • 13
  • 63
  • 115

1 Answers1

1

You forget to inject $location in your controller

Try like this

function MainCtrl(user, auth,$location) {
Anik Islam Abhi
  • 25,137
  • 8
  • 58
  • 80