AngularJS功能DI参数

时间:2014-10-06 18:36:00

标签: javascript angularjs dependency-injection

我正在构建应用程序身份验证部分的提供程序。我在使用依赖注入器时将值传递给函数时遇到了麻烦。

angular.module('security.authorization', ['security.service'])
.provider('securityAuthorization', {
    requireRole: ['securityAuthorization', 'rolesAllowed', function(securityAuthorization, rolesAllowed) {
        return securityAuthorization.requireRole(rolesAllowed);
    }],

    $get: ['security', '$q', function(security, $q) {
        return {
            requireRole: function(rolesAllowed) {
                console.log(rolesAllowed);
            }
        };
    }]
});

我正在尝试从app.config $ routeProvider调用“requireRole”。

.when('/map', { controller: 'MapCtrl', templateUrl: 'partials/user/map.html',
        resolve: {
            authenticatedUser: securityAuthorizationProvider.requireRole('user')
        }
    })

但它给了我

  

未捕获错误:[$ injector:modulerr]无法实例化模块   myApp由于:TypeError:object不是函数

更新

app.config(function($routeProvider, $httpProvider, securityAuthorization) {
    $routeProvider
    .when('/map', { controller: 'MapCtrl', templateUrl: 'partials/user/map.html',
        resolve: {
            authenticatedUser: ['securityAuthorization', function(){
                return securityAuthorization.requireRole('user');
            }]
        }
    });
}); 

错误:

  

错误:[$ injector:unpr]未知提供者:securityAuthorization

1 个答案:

答案 0 :(得分:1)

您需要将函数作为第二个参数传递给provider https://docs.angularjs.org/guide/providers#provider-recipe

由于您似乎没有使用securityAuthorizationProvider,我已将此更改为使用工厂。如果您定义了工厂配方,则会自动创建一个将$ get方法设置为工厂函数的空提供程序类型。

.factory('securityAuthorization', ['security', '$q', function(security, $q) {
    return {
        requireRole: function(rolesAllowed) {
            // require that there is an authenticated user
            return security.auth().then(function(user) {
                if (!security.isAuthenticated()) {
                    $q.reject('/signin');
                }
                else {
                    // check if role is allowed
                    if (rolesAllowed.indexOf(user.role) == -1) {
                        $q.reject('/signin');
                    }
                    else {
                        if (user.defaultPassword) {
                            $q.reject('/change-password');
                        }
                        else {
                            // This was in the original post, but $q doesn't have resolve
                            $q.resolve();
                        }
                    }
                }
            }, function(){
                $q.reject('/signin');
            });
        }
    };
}]);

如何设置路由

app.config(function($routeProvider, $httpProvider) {
    $routeProvider
        .when('/map', { controller: 'MapCtrl', templateUrl: 'partials/user/map.html',
            resolve: {
                authenticatedUser: ['securityAuthorization', function(securityAuthorization){
                    return securityAuthorization.requireRole('user');
                }]
            }
        });
});