导入ngUpload时的角度误差:[ng:area]和[$ injector:unpr]

时间:2014-04-10 10:42:11

标签: javascript angularjs angularjs-directive

我正在尝试导入角度指令ngUpload

但是当打开html页面时我有这个错误:

Error: [$injector:unpr] http://errors.angularjs.org/1.2.15-build.2378+sha.9335378/$injector/unpr?p0=AuthProvider%20%3C-%20Auth%20%3C-%20accessLevelDirective
http://localhost:8000/components/angular/angular.min.js:32:150
c@http://localhost:8000/components/angular/angular.min.js:30:201
http://localhost:8000/components/angular/angular.min.js:32:196
c@http://localhost:8000/components/angular/angular.min.js:30:201
d@http://localhost:8000/components/angular/angular.min.js:30:486
http://localhost:8000/components/angular/angular.min.js:40:166
forEach@[native code]

Error: [ng:areq] http://errors.angularjs.org/1.2.15-build.2378+sha.9335378/ng/areq?p0=NavCtrl&p1=not%20a%20function%2C%20got%20undefined
xb@http://localhost:8000/components/angular/angular.min.js:18:391
Pa@http://localhost:8000/components/angular/angular.min.js:18:449
http://localhost:8000/components/angular/angular.min.js:63:96
http://localhost:8000/components/angular/angular.min.js:49:196
r@http://localhost:8000/components/angular/angular.min.js:7:408
A@http://localhost:8000/components/angular/angular.min.js:49:62
h@http://localhost:8000/components/angular/angular.min.js:43:84
A@http://localhost:8000/components/angular/angular.min.js:49:468
h@http://localhost:8000/components/angular/angular.min.js:43:84

发生此错误的控制器是:

angular.module('angular-client-side-auth',['ngUpload'])
.controller('homeCtrl',
            ['$rootScope', '$scope', 'Users','$location', 'Auth', function($rootScope, $scope, Users,$location, Auth) {
                $scope.isAdmin = function(){
                    console.log(JSON.stringify(Auth.user.role.title));
                    return Auth.user.role.title == "admin";
                };
                $scope.complete = function(content) {
                    console.log(content); // process content
                };
                Users.getMe(function(res) {
                    $scope.user= res;
                }, function(err) {
                    $rootScope.error = "Failed to fetch me.";
                    $scope.loading = false;
                });

            }]);
有人有什么想法吗? THX

----------- -----------更新

这是我的Auth声明

angular.module('angular-client-side-auth')
.factory('Auth', function($http, $cookieStore){

    var accessLevels = routingConfig.accessLevels
        , userRoles = routingConfig.userRoles
        , currentUser = $cookieStore.get('user') || { username: '', role: userRoles.public };

    $cookieStore.remove('user');

    function changeUser(user) {
        angular.extend(currentUser, user);
    };

    return {
        authorize: function(accessLevel, role) {
            if(role === undefined)
                role = currentUser.role;

            return accessLevel.bitMask & role.bitMask;
        },
        isLoggedIn: function(user) {
            if(user === undefined)
                user = currentUser;
            return user.role.title == userRoles.manager.title || user.role.title == userRoles.admin.title;
        },
        register: function(user, success, error) {
            $http.post('/register', user).success(function(res) {
                changeUser(res);
                success();
            }).error(error);
        },
        login: function(user, success, error) {
            $http.post('/login', user).success(function(user){
                changeUser(user);
                success(user);
            }).error(error);
        },
        logout: function(success, error) {
            $http.post('/logout').success(function(){
                changeUser({
                    username: '',
                    role: userRoles.public
                });
                success();
            }).error(error);
        },
        accessLevels: accessLevels,
        userRoles: userRoles,
        user: currentUser
    };
});

这是NavCtrl声明

angular.module('angular-client-side-auth')
.controller('NavCtrl', ['$rootScope', '$scope', '$location', 'Auth', function($rootScope, $scope, $location, Auth) {
    $scope.user = Auth.user;
    $scope.userRoles = Auth.userRoles;
    $scope.accessLevels = Auth.accessLevels;

    $scope.logout = function() {
        Auth.logout(function() {
            $location.path('/login');
        }, function() {
            $rootScope.error = "Failed to logout, sorry";

        });
    };
}]);

但如果我不导入ngupload,我的角度应用程序没有任何问题

1 个答案:

答案 0 :(得分:1)

这看起来像是一个订购问题。

您需要首先声明模块及其依赖项:

angular.module('angular-client-side-auth',['ngUpload']);

然后,您需要在其他控制器/服务/指令中使用之前添加其他控制器和服务:

angular.module('angular-client-side-auth').factory('Auth', ...);
angular.module('angular-client-side-auth').controller('homeCtrl', ...);
angular.module('angular-client-side-auth').controller('navCtrl', ...);

您尝试在新申请的模块上立即声明控制器,该模块尚未注册Auth工厂。