Angularjs:限制访问除少数所有页面 [英] Angularjs: restrict access to all pages except few

查看:154
本文介绍了Angularjs:限制访问除少数所有页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是UI的路由器和智威汤逊Angularjs(这是基于令牌身份验证的工具),并希望实现认证。

I'm using Angularjs with ui-router and JWT(which is a tool for token based authentication) and want to implement authentication.

说我要限制访​​问我的所有页面,除了三:/家,/约和/登录。我怎样才能达到这样的效果?

Say i want to restrict access to all my pages except three: "/home", "/about" and "/login". How can I achieve this effect?

推荐答案

它的工作!享受:)

var scotchApp = angular.module('scotchApp', ['ngCookies','ngRoute','ui.bootstrap']);

    scotchApp.run(['$rootScope', '$location', 'Auth', function($rootScope, $location, Auth) {
        $rootScope.$on('$routeChangeStart', function(event, current) {
            $rootScope.pageTitle = current.$$route.title;
            if (!Auth.isLoggedIn() && current.$$route.withLogin || Auth.isLoggedIn() && current.$$route.withoutLogin) {
                event.preventDefault();
                $location.path('/');
            }
        });
    }]);

    scotchApp.config(function($routeProvider) {
        $routeProvider
        // route for the home page

            .when('/list', {
            templateUrl: 'pages/list.html',
            controller: 'mainController',
            title: 'User List',
            withLogin: true,
        })

        .when('/register', {
            templateUrl: 'pages/register.html',
            controller: 'mainController',
            title: 'Register',
            withoutLogin: true,
        });
    });

这篇关于Angularjs:限制访问除少数所有页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆