Rahul Jain Rahul Jain - 2 months ago 23
AngularJS Question

AngularJS - How to use services which are defined in one module, in the controller defined in another module

I am beginner to the Angular JS.
I made a service module to define only services :



(function() {
var srv = angular.module("service_module",[]);
srv.service("HotelService", function($http, $q) {
//My code written here
});
})();





And I created module in which Controllers are written :



(function() {
var controllers = angular.module("controllers_module",["service_module"]);

controllers.controller("searchFormController", function($rootScope, $scope) {
//My code here
});

})();





Now when I run my code, I get this error:

ReferenceError: HotelService is not defined


Please tell me where I am wrong.

Answer

you need to inject that service inside your controller to use it.

controllers.controller("searchFormController", function($rootScope, $scope,HotelService ) {
    //My code here
});
Comments