Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
AngularJS部分视图安全性_Angularjs_Angularjs Routing_Angularjs Factory - Fatal编程技术网

AngularJS部分视图安全性

AngularJS部分视图安全性,angularjs,angularjs-routing,angularjs-factory,Angularjs,Angularjs Routing,Angularjs Factory,我试图保护部分视图,这样用户在没有登录的情况下就不能切换视图,但会遇到问题。代码如下: var loginApp = angular.module('loginApp', [ 'ngCookies', 'ngResource', 'ngSanitize', 'ngRoute' ]) // // // // --------------

我试图保护部分视图,这样用户在没有登录的情况下就不能切换视图,但会遇到问题。代码如下:

       var loginApp = angular.module('loginApp', [
            'ngCookies',
            'ngResource',
            'ngSanitize',
            'ngRoute'
        ])

    //
    //
    //
    //   -------------- Cannot get this to work ------------
    //
    //
    //
    //    loginApp.factory('authInterceptor', function ($q, $location) {
    //        debugger;
    //        return {
    //            request: function (config) {
    //                config.headers = config.headers | {};
    //                if (localStorage.auth_token) {
    //                    config.headers.token = localStorage.auth_token;
    //                }
    //                return config;
    //            },
    //            responseError: function (response) {
    //
    //                if (response.status === 401 || response.status === 500) {
    //                    $location.path('/');
    //                }
    //                return $q.reject(response);
    //            }
    //        }
    //    })
    //
    //    loginApp.config(function ($httpProvider) {
    //        $httpProvider.interceptors.push('authInterceptor');
    //    })


        loginApp.config(function ($routeProvider) {
            $routeProvider
                .when('/', {
                    templateUrl: 'views/login.html',
                    controller: 'loginController'
                })

                .when('/expertView', {
                    templateUrl: 'views/b.html',
                    controller: 'bViewController'
                })

        });


        loginApp.controller('bViewController', function ($scope) {
                  //$scope.message = 'Everyone come and see how good I look!';
        });



        var loginController = function ($scope, $http, $location) {
            $scope.user = {};
            $scope.user.username = "name";
            $scope.user.userID = "123456";
            $scope.user.password = "444444444";
            $scope.user.ui = "true";

            $scope.user.submitForm = function (item, event) {
                var data = {
                    userID: $scope.user.userID,
                    password: $scope.user.password,
                    ui: $scope.user.ui
                };

                $http({
                    url: '/api/v1/auth/login',
                    method: "POST",
                    dataType: "json",
                    data: data,
                    headers: {
                        'Accept': 'application/json, text/javascript',
                        'Content-Type': 'application/json; charset=utf-8'
                    }

                }).success(function (data, status, headers, config) {
                    console.log("Success!");
                }).error(function (data, status, headers, config) {
                    console.log("Submitting to Server failed!");
                });
            }
        }

我只需要保护视图,并确保用户在未登录的情况下无法(切换)访问视图。

首先创建一个常数,该常数可以确定每个路由的访问级别,例如

 angular.module("App")
.constant('USER_ROLES', {
  logedIn : 'true'
});
然后将它们添加到管线的定义中,作为

.when('/write',{
    templateUrl:'templates/write.html',
    access_level:USER_ROLES.logedIn
})

之后,在运行函数中检查(“$locationChangeStart”事件中的
$rootScope.$on,在该事件中,您可以通过
var location=$location.path();var route=$route.routes[location]访问路由;
然后通过
路由访问用户角色。访问_level;

当你说你不能让它工作时,你的确切意思是什么?你遇到了什么样的错误,或者你看到了什么样的行为?代码将内容类型转换为纯文本,并停止http发布。我只需要一种简单的方法来保护视图。我无法解释。下面是一个很好的解释:非常感谢。你能把它放在上面的代码中,这样我就可以一起看到它了。这将非常有帮助。