2

I am using angularjs and nodejs for my project. Now after I do my authentication using background call. Now after I receive my successful authentication, how do I redirect the user to dashboard? Here is my login div:

  <div ng-controller="loginCtrl" class="control">
    <form role="form" name="docRegForm" ng-submit="login()" enctype="multipart/form-data" class="col-xs-11 div-center">
      <div class="input-group">
        <input id="exampleInputEmail1" type="text" placeholder="Username" ng-model="user.username" class="form-control"/>
      </div>
      <div class="input-group">
        <input id="exampleInputPassword1" type="password" placeholder="Password" ng-model="user.password" class="form-control"/>
      </div>
      <div class="col-xs-12 div-center">
        <button type="submit" class="btn btn-danger full-width">LOGIN</button>

And my angular controller is:

app.controller('loginCtrl', function ($scope, $http, $window) {
  $scope.message = '';

  $scope.login = function () {
    $http
      .post('/authenticate', $scope.user)
      .success(function (data, status, headers, config) {
        $window.localStorage.nimbusToken = data.token;
        console.log($window.localStorage.token);
        $scope.message = 'Welcome';  
        };
        alerts[data.status];
      })
      .error(function (data, status, headers, config) {
        // Erase the token if the user fails to log in
        alert("failure");
        delete $window.localStorage.token;
        // Handle login errors here
        $scope.message = 'Error: Invalid user or password';
      });
  };
});

Now after the login, I have to redirect to dashboard or to relogin, if failed login. How do I do that?

6
  • use $location.path('/').. Commented Jan 19, 2015 at 12:55
  • i tried that. but only the url in address bar changes. but the page does not navigate. Commented Jan 19, 2015 at 13:10
  • 1
    than try: window.location.href = "your path"; Commented Jan 19, 2015 at 13:12
  • 1
    or use: window.location.replace("your path."); Commented Jan 19, 2015 at 13:14
  • yep... thanks a lot. Both did the trick. which one is better approach? setting the window.location.href or replacing window.location.replace()? Commented Jan 19, 2015 at 13:17

2 Answers 2

2

If $location.path(''), not works for you, than try these:

// similar behavior as an HTTP redirect

window.location.replace("your path.");

or:

 window.location.replace("your path.")
Sign up to request clarification or add additional context in comments.

2 Comments

This will reload the whole page.
Try window.location = "your_path" Or just $state.go('your_path'). $state.go is preferred, because it's only change the route and not reload the whole page
1

In success use:-

$location.path("/dashboard");

In error use:-

$location.path("/login");

1 Comment

I did that and only the url in the address bar changes, but does not redirect.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.