Hamurabi Araujo Hamurabi Araujo - 1 month ago 12
AngularJS Question

Unknown provider on calling a factory on controller "Error: [$injector:unpr]"

I have with a problem on call a factory in controller. The factory should do the REST requests on controller calls, but this error is occurring:

Error: [$injector:unpr] Unknown provider: SearchServiceProvider <- SearchService <- HomeController


I'm trying solve this for some days, but nothing fix this.

busca.app.js

(function () {
'use strict';

angular
.module('saldanhaTransporte', ['ngMessages'])
.factory('SearchService', Service);

function Service($http) {
var service = {};

service.SearchPendingDeliveries = SearchPendingDeliveries;

return service;

function SearchPendingDeliveries(docNumberClient, callback) {
//some code
}
}
})();


home.controller.js

(function() {
'use strict';

angular
.module('saldanhaTranporte', [])
.controller('HomeController', HomeController);

function HomeController(SearchService) {
var vm = this;

vm.searchDeliveriesFromUser = searchDeliveriesFromUser;

function searchDeliveriesFromUser() {
SearchService.SearchPendingDeliveries(vm.identifierNumber, function (result) {
if (result === true) {
console.log(true);
} else {
console.log(false);
}
});
};
};
})();


index.html

...
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.js"></script>
<script src="https://code.angularjs.org/1.5.8/angular-messages.js"></script>
<script src="service/busca.app.js"></script>
<script src="home/home.controller.js"></script>
...

Answer

You are declaring the same module twice

Try changing

angular
    .module('saldanhaTranporte', [])
    .controller('HomeController', HomeController);

To

angular
    .module('saldanhaTranporte')
    .controller('HomeController', HomeController);

Without the second argument in module() it is a getter not a setter and returns reference to an existing module

Comments