Search code examples
javascriptangularjsangularjs-factoryangularjs-injector

Angularjs factory inject error


I'm using angularJS and when I'm injecting a Factory I get error:

app.js :

angular.module('myapp', [])

myfactory.js :

angular.module('myapp', [])
.factory('httpService', function($http, $timeout) {

});

Controller: test.js :

angular.module('myapp', [])
.controller('test',  function($scope, $timeout, $sce, $http, httpService) {

  $scope.submit = function() {
  }
});

When I add httpService I get error. Everything seems to be right, I even use this factory in all projects. Error:

angular.min.js:92 Error: [$injector:unpr] http://errors.angularjs.org/1.2.25/$injector/unpr?p0=httpServiceProvider%20%3C-%20httpService
at Error (native)
at http://localhost:81/chah/assets/js/angularjs/angular.min.js:6:450
at http://localhost:81/chah/assets/js/angularjs/angular.min.js:36:202
at Object.c [as get] (http://localhost:81/chah/assets/js/angularjs/angular.min.js:34:305)
at http://localhost:81/chah/assets/js/angularjs/angular.min.js:36:270
at c (http://localhost:81/chah/assets/js/angularjs/angular.min.js:34:305)
at d (http://localhost:81/chah/assets/js/angularjs/angular.min.js:35:6)
at Object.instantiate (http://localhost:81/chah/assets/js/angularjs/angular.min.js:35:165)
at http://localhost:81/chah/assets/js/angularjs/angular.min.js:67:419
at http://localhost:81/chah/assets/js/angularjs/angular.min.js:54:25

Solution

  • Check the link in your error (https://docs.angularjs.org/error/$injector/unpr?p0=httpServiceProvider%20%3C-%20httpService):

    You create module multiple times:

    angular.module('myapp', [])
    

    You should do it once. Then use without []

    angular.module('myapp').factory ...
    angular.module('myapp').controller ...