tumulr
tumulr

Reputation: 133

Angular Js $injector:unpr error

I am new to angular and I was trying to implement a custom service. Code is below:

app.js

angular.module('hrg', [
  'ui.router',
  'ui.bootstrap',
  'ngTouch',
  'ngAnimate',
  'ngCookies',
  'hrg.home'
])

.service('userService', ['', function(){
//data
}])

home.js

angular.module('hrg.home', ['ui.router'])

.controller('HomeController', ['$scope','$http','$cookies','$state','consts', 'userService',
  function($scope, $http, $cookies, $state, consts, userService) {
  //data
}])

index.html

<script src="app.js"></script>
<script src="components/home/home.js"></script>

The error is: [$injector:unpr] http://errors.angularjs.org/1.5.0/$injector/unpr?p0=userServiceProvider%20%3C-%20userService%20%3C-%20HomeController

What is it that I am doing wrong?

Upvotes: 1

Views: 560

Answers (1)

Pankaj Parkar
Pankaj Parkar

Reputation: 136134

You have put a '' inside userService inline DI array, which ask for '' that is culprit of your issue. Because when you inject userService dependency inside controller, angular injector goes for creating an instance of userService service.

When resolving dependency from userService DI array, it passes '' to injector, then first it creates provider name by concatenate it with Provider suffix like ''+ 'Provider with result to Provider as dependency name & obviously there is no such dependency present. So angular is throwing an exception $injector/unpr? means Injector unknown provider

Change below service code

                    //VVVVVVV//unwanted '' blank dependency
.service('userService', ['', function(){
   //data
}])

to

.service('userService', [function(){ //removed `''` dependency
   //data
}])

Upvotes: 1

Related Questions