Search code examples
angularjsangularjs-service

AngularJS [$injector:unpr] Unknown provider


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] 

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


Solution

  • 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;
            }
        };
    
    });
    

    Demo

    Quote from Doc:-

    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.