如何根据angularjs中的条件从控制器重定向页面?

时间:2015-11-03 00:19:20

标签: angularjs angularjs-directive angularjs-scope angular-ui-router

我一直在为每个$ http请求和网址更改进行身份验证。因此,我已经实现了URL更改身份验证的代码和平

/** login-controller.js file **/

//when the form is submitted
$scope.submit = function() {
    console.log("enter login submit");
    $scope.submitted = true;
    if (!$scope.loginForm.$invalid) {
        $scope.login($scope.credentials);
    } else {
        $scope.error = true;
        $scope.error_des = '';
        return;
    }
};

// if a session exists for current user (page was refreshed)
// log him in again
if ($window.sessionStorage["userInfo"]) {
        var credentials = JSON.parse($window.sessionStorage["userInfo"]);
        $scope.login(credentials);
};

//Performs the login function, by sending a request to the server with the Auth service
$scope.login = function(credentials) {

    $scope.error = false;
    Auth.login(credentials, function(user) {
        //success function
        $mdDialog.hide();
        $state.go("admin-panel.default.home");
    }, function(err) {
        console.log("error");
        $scope.error_des = err.msg;
        $scope.error = true;
    });
};

一切都按照我的预期工作,但在重新编码相同的页面时,它总是会重定向到主页(http://localhost:3000/#/home).i知道它发生的原因是

$state.go("admin-panel.default.home");

麻烦的是,我想根据登录方式重定向页面如果用户通过提交的表单登录,将被重定向到$ state.go(“admin-panel.default.home”); 如果用户通过会话登录,则当前用户将被重定向到$ state.go(“admin-panel.default。”+ $ current_page );

如何实现呢?请帮帮我????

1 个答案:

答案 0 :(得分:0)

If form submit : var formsubmitted = true;
if login via session : formsubmitted = false
 and while routing you can check condition 
if(formsubmitted == true)
   $state.go("admin-panel.default.home");
else
$state.go("admin-panel.default."+$current_page);
//to get current page path  you can use $location.path();