gqmartins gqmartins - 1 month ago 17
AngularJS Question

Service is not defined

I'm having an error while using AngularJS, I can't import a Service from one module to another. I have a Service called MenuDataService in module Data, that I want to use in module MenuApp, and when I try to do it, it gives an error with the following link https://docs.angularjs.org/error/$injector/unpr?p0=MenuDataServiceProvider%20%3C-%20MenuDataService%20%3C-%20CategoriesController.

src/data-module/data.module.js:

angular.module('Data', []);


src/data-module/menudata.service.js:

angular.module('Data')
.constant('CATEGORIES_URI', 'some_uri')
.service('MenuDataService ', MenuDataService);

MenuDataService.$inject = ['$http', 'CATEGORIES_URI'];
function MenuDataService($http, CATEGORIES_URI) {
var service = this;

service.getAllCategories = function () {
return httpRequest(CATEGORIES_URI);
};
};


src/menuapp-module/menuapp.module.js:

angular.module('MenuApp', ['Data']);


src/menuapp-module/categories.controller.js:

angular.module('MenuApp')
.controller('CategoriesController', CategoriesController);

CategoriesController.$inject = ['MenuDataService'];
function CategoriesController(MenuDataService) {
console.log('CATEGORIES CONTROLLER');
};


index.html:

<script type="text/javascript" src="./lib/angular.min.js"></script>
<script type="text/javascript" src="./src/data-module/data.module.js"></script>
<script type="text/javascript" src="./src/data-module/menudata.service.js"></script>
<script type="text/javascript" src="./src/menuapp-module/menuapp.module.js"></script>
<script type="text/javascript" src="./src/menuapp-module/categories.controller.js"></script>


Any help would be great since I don't know what I'm doing wrong...

Thank you very much!

Answer

There is an extra space in the service name that you define.

.service('MenuDataService ', MenuDataService);
                         ^
Comments