在Angular Config中获取DB值

时间:2016-11-10 18:59:22

标签: angularjs web-applications angular-providers

我有一个使用角度的应用。在app.config中我需要设置我的路线,但是我想要授权我的路线,因为不是每个使用该应用的人都能看到每一页。

所以我已经尝试建立一个工厂,给出一个布尔值来告诉应用程序该人是否可以看到路线,并了解到我不能将工厂注入配置。

所以我创建了一个可以注入配置的提供程序:

(function () {
    'use strict';

    angular.module('services')
    .factory('Auth', ['$http', function AuthFactory($http) {
        return {
            LinkAuth: function (Name) {
                return $http({ method: 'GET', url: '/Dashboard/AuthorizeNavItem', data: { Name: Name } });
            }
        }
    }]);


    angular.module('services')
    .provider('AuthProvider', ['Auth', function (Auth) {
        var allowed = false;

        this.$get = function (Name) {
            Auth.LinkAuth(Name).success(function (data) {
                allowed = data.Authorized;
            });

            return allowed;
        }

    }]);
})();

我的app.config:

(function () {
    'use strict';

    angular.module('app.it', [])
    .config(['$stateProvider', 'msNavigationServiceProvider', 'AuthProvider', function ($stateProvider, msNavigationServiceProvider, AuthProvider) {

        $stateProvider
        .state('app.it', {
            abstract: true,
            url: '/information-technology',
        })
        .state('app.it.users', {
            url: '/users',
            views: {
                'content@app': {
                    templateUrl: '/IT/Users',
                    controller: 'ITUserController as vm'
                }
            }
        });

        /*
            We need to get a bool to say if a user is part of a group that can see this page.
        */
        var allowed = true;//I want the provider $get method to return that bool here

        if (allowed)
        {
           //This builds the navigation bar
            msNavigationServiceProvider.saveItem('app.authroute', {
                title: 'Authorized Route',
                icon: 'icon-monitor',
                weight: 2
            });

            //This builds the navigation bar
            msNavigationServiceProvider.saveItem('app.authroute.route', {
                title: 'Route',
                state: 'app.authroute.route'
            });
        }


    }]);

})();

如何访问AuthProvider $ get并将bool存储在配置中的变量中?

0 个答案:

没有答案