应用程序在angularjs中初始化时重定向

时间:2013-05-17 08:59:14

标签: angularjs angularjs-routing

在应用初始化时首页加载我想将用户重定向到登录页面。我认为代码的相关部分是这个

$rootScope.$on("$routeChangeStart", function (event, next, current) {
    alert("change location");

    $location.path('/login');
});

它基于https://github.com/fnakstad/angular-client-side-auth/blob/master/app/js/app.js问题是在页面加载时触发警报但位置不会改变。我必须单击我的应用程序的导航项,然后将调用登录操作并更改路径。

.run(['$rootScope', '$location', '$cookieStore', function ($rootScope,
                                                     $location, $cookieStore) {

    $location.path('/login');

    $rootScope.$on("$routeChangeStart", function (event, next, current) {

        $location.path('/login');

    });

    $rootScope.appInitialized = true;
}]);

但这似乎有点多余。为什么会触发警报但不会改变位置?

为什么整页加载时位置不会改变?如何解决这个问题?

完整代码http://jsfiddle.net/qfSC3/但小提琴不起作用。

1 个答案:

答案 0 :(得分:3)

尝试使用$ locationChangeStart事件

$scope.$on("$locationChangeStart", function(event){
    event.preventDefault();
})

基于这个问题:AngularJS - Detecting, stalling, and cancelling route changes

相关问题