AngularJS [$injector:unpr] 未知提供者 [英] AngularJS [$injector:unpr] Unknown provider

查看:32
本文介绍了AngularJS [$injector:unpr] 未知提供者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将服务注入控制器,但出现以下错误:

I am trying to inject a service into controller, and i am getting following error:

Error: [$injector:unpr] Unknown provider: employeeServiceProvider <- employeeService
http://errors.angularjs.org/1.3.0-beta.17/$injector/unpr?p0=employeeServiceProvider%20%3C-%20employeeService
    at http://localhost:9082/angularJSDemo/js/lib/angular.js:78:12
    at http://localhost:9082/angularJSDemo/js/lib/angular.js:3894:19
    at Object.getService [as get] 

这是代码的plunker.任何帮助将不胜感激.

Here is plunker for code. Any help would be appreciated.

推荐答案

您在任何地方重复 angular.module('demoApp', []) 它将清除附加到模块的任何实体已经添加并重新创建模块,在模块初始化后,您应该使用其引用或仅使用 angular.module('demoApp').service... 使用此将检索您可以访问的模块添加服务等...

You are repeating angular.module('demoApp', []) everywhere it will clear out any entities attached to the module that has been added already and recreate the module, after module initialization you should use its reference or just use angular.module('demoApp').service... using this will retrieve the module to which you can add services etc...

var module = angular.module('demoApp', []).controller('employeeController', function($scope, employeeService) {
    $scope.employees = employeeService.getData();
});


module.factory('employeeService', function (){
    return {
        getData : function(){
            var employees = [{name: 'John Doe', id: '1'}, 
                                {name: 'Mary Homes', id: '2'},
                                {name: 'Chris Karl', id: '3'}
                                ];

            return employees;
        }
    };

});

演示

引用自 Doc:-

请注意,使用 angular.module('myModule', []) 将创建模块 myModule 并覆盖任何名为 myModule 的现有模块.使用 angular.module('myModule') 检索现有模块.

Beware that using angular.module('myModule', []) will create the module myModule and overwrite any existing module named myModule. Use angular.module('myModule') to retrieve an existing module.

这篇关于AngularJS [$injector:unpr] 未知提供者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆