1

我正在尝试注入我的服务,但我不断收到 unpr 错误。我遵循了 angularjs 文档,但似乎没有解决它。我不知道为什么这个错误不断出现。


我的服务

(function(){
'use strict';

angular
    .module('omicron.projects.services')
    .factory('Projects', Projects);

Projects.$inject = ['$resource', '$scope'];

/**
 *
 * @param $resource
 * @constructor
 */
function Projects($resource){
    return $resource('/api/projects/:userid', {}, {
        get: {method: 'GET'},
        query: {method: 'GET'},
        update: {method: 'PUT'},
        save: {method: 'POST'},
        delete: {method: 'DELETE'}
    },{
        stripTrailingSlashed: false
    });
}

})();

我的控制器,调用服务

(function() {
'use strict';

angular
    .module('omicron.projects.controllers')
    .controller('ProjectController', ProjectController);

ProjectController.$inject = ['$scope', 'Projects'];

function ProjectController($scope, Projects) {
    var vm = this;

     vm.projects = Projects.get({userid: $scope.user}, function(){
        console.log(projects);
    });
}

})();

我的模块定义

    /**
 * Created by shahab on 15-09-03.
 */
(function(){
'use strict';

  angular
   .module('omicron.projects', [
       'omicron.projects.controllers',
       'omicron.projects.directives',
       'omicron.projects.services'
   ]);


angular
    .module('omicron.projects.services', ['ngCookies', 'ngResource']);

angular
    .module('omicron.projects.controllers', []);

angular
    .module('omicron.projects.directives', []);

})();

我的错误:

错误:[$injector:unpr] 未知提供者:$resourceProvider <- $resource <- Projects

4

1 回答 1

2

工厂无法访问范围

Projects.$inject = ['$resource', '$scope']; //remove scope from here

应该

Projects.$inject = ['$resource']; 

此外,您需要确保在应用程序主模块中添加了angular-resource.js库和ngResource模块omicron.projects

你应该在你的app.js

angular.module('omicron.projects', [
     'omicron.projects.services', 
     'omicron.projects.controllers', 
     'omicron.projects.directives', 
     'ngResource'
])
于 2015-09-04T20:38:55.240 回答