无法将提供程序注入配置块

时间:2015-04-26 17:42:01

标签: angularjs dependency-injection

我无法在配置部分注入提供程序,这里是我的代码:

 <!-- they are placed in that order -->
            <script src="app/components/department/DepartmentController.js"></script>
            <script src="app/components/department/DepartmentProvider.js"></script>
            <script src="app/components/department/DepartmentService.js"></script>


            <script src="app/components/department/support/SupportController.js"></script>
            <script src="app/components/department/support/SupportRouter.js"></script>

Department.js

var departmentModule = angular.module('module.department', ['ngResource']);

    // departmentModule.controller('DepartmentController', function($scope) {

 //   nothing here

    // });

DepartmentService.js:

departmentModule.factory('Department', function($http) {

            return {
                getAll : function(){
                        return $http.get('/api/departments');
                    },
                getByName : function(name){
                        return $http.get('/api/department/'+name);
                    }
                };

});

DepartmentProvider.js:

departmentModule.provider('Department', function() {
     var name = 'marwen';

     this.$get = function() {
         return {
            name: name
         };
     };
});

SupportRouter.js:

supportModule.config(function($stateProvider, $urlRouterProvider,$locationProvider,DepartmentProvider) {

...

我收到此错误:Unknown provider: DepartmentProvider

2 个答案:

答案 0 :(得分:1)

尝试将supportModule.config(function($stateProvider, $urlRouterProvider,$locationProvider,DepartmentProvider)更改为supportModule.config(function($stateProvider, $urlRouterProvider,$locationProvider,Department)

或将您的提供商名称从Department更改为DepartmentProvider

答案 1 :(得分:1)

Department服务提供商已被Department服务覆盖,您需要为提供商&amp;提供不同的名称。服务,两者不能同名,最新的一个会注册。当您在提供商之后加载服务时。服务在Angular App&amp;提供程序在配置中不可用。