如何在angularjs中的ajax请求后重定向?

时间:2015-01-19 12:53:21

标签: ajax angularjs node.js

我正在为我的项目使用angularjs和nodejs。现在我使用后台调用进行身份验证后。我收到成功的身份验证后,如何将用户重定向到仪表板?这是我的登录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>

我的角度控制器是:

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

现在登录后,如果登录失败,我必须重定向到仪表板或重新登录。我该怎么做?

2 个答案:

答案 0 :(得分:2)

如果$location.path('')不适合您,请尝试以下方法:

// similar behavior as an HTTP redirect

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

或:

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

答案 1 :(得分:1)

成功使用: -

$ location.path( “/仪表盘”);

错误使用: -

$ location.path( “/登录”);