且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

AngularJs - ***的方式来限制访问“登录”用户

更新时间:2022-12-31 08:23:14

您可以拦截路由变化如你所说,并采取相应的行动,使用下面的示例作为基础:

You can intercept route changes as you suggested and act accordingly, using the following example as a basis:

    $rootScope.$on('$routeChangeStart', function (event, next) {
        var userAuthenticated = ...; /* Check if the user is logged in */

        if (!userAuthenticated && !next.isLogin) {
            /* You can save the user's location to take him back to the same page after he has logged-in */
            $rootScope.savedLocation = $location.url();

            $location.path('/User/LoginUser');
        }
    });

此外,添加 isLogin:真正的来登录页面的路由定义,像这样的:

Also, add isLogin: true to the route definition of your login page, like this:

$routeProvider
    // LOGIN
    .when('/User/LoginUser', {templateUrl: 'views/user/login.html',controller: 'loginCtrl', isLogin: true})

祝你的项目!