Angular ui路由器没有使用$ location.path()

时间:2017-04-21 13:13:05

标签: angularjs angular-ui-router

我尝试在模块的run方法中定义一些简单的路由安全性。

但是我尝试过,虽然我没有登录,但我被重定向到安全路线:

            // Secure routes
            $rootScope.$on("$stateChangeStart", function (event, toState, toParams, fromState, fromParams) {
                if ("login" === toState.name) {
                    return;
                }

                let authorized = toState.data ? toState.data.authorization : false;

                if (!securityService.isAuthenticated && authorized !== true) {
                    $location.path("/login");
                }
            });

我尝试了$location.path("/login");$location.url("/login");window.location.href = "/login";,同时我尝试了$rootScope.$apply等等。这些都不能让我重定向到登录页面。

编辑:更详细的视图

namespace testWeb {
    class AppComponent implements ng.IComponentOptions {
        template = `
        <div class="root"><div ui-view=""></div>
        </div>`;
        controller = AppController;
    }

    interface IAppController {
    }

    export class AppController implements IAppController {
    }

    angular.module("test", [
        "ui.router"
    ]).run(["$rootScope", "$state",
        function ($rootScope, $state) {
            // Secure routes
            $rootScope.$on("$stateChangeStart", function (event, toState, toParams, fromState, fromParams) {
                if ("login" === toState.name) {
                    return;
                }

                let authorized = toState.data ? toState.data.authorization : false;

                if (!securityService.isAuthenticated && authorized !== true) {
                        $state.go("login");
                }
            });
        }]).component("root", new AppComponent())
        .config(($stateProvider, $urlRouterProvider, $translateProvider) => {
            "ngInject";
            $urlRouterProvider.otherwise("/xyz");
            $stateProvider
                .state("login", {
                    url: "/login",
                    templateUrl: "scripts/components/login/login.html"
                });
        });
}

P.S。 我在TypeScript

中使用Angular 1.6

2 个答案:

答案 0 :(得分:2)

如果你使用的是ui-router,你应该使用$ state.go(&#39; state_name&#39;)

 $rootScope.$on("$stateChangeStart", function (event, $state, toState, toParams, fromState, fromParams) {
                if ("login" === toState.name) {
                    return;
                }

                let authorized = toState.data ? toState.data.authorization : false;

                if (!securityService.isAuthenticated && authorized !== true) {
                    $state.go("login");
                }
            });

答案 1 :(得分:-1)

好的,解决方案很简单,因为使用event.preventDefault()阻止了事件的进一步执行:

$rootScope.$on("$stateChangeStart", function (event, toState, toParams, fromState, fromParams) {
            if ("login" === toState.name) {
                return;
            }

            let authorizationRequired = toState.data ? toState.data.authorization : true;

            if (!securityService.isAuthenticated && authorizationRequired) {
                event.preventDefault();
                $state.go("login");
            }
        });