D dr
D dr

Reputation: 31

Angular factory and Unknown provider error

I have 2 Angular modules. rootModule:

var myModule = angular.module('rootModule', []);

myModule.factory('mySharedService', function($rootScope) {
    var sharedService = {};
    return sharedService;
});

myModule.factory('getMonthlyHistoryService', function ($http, $q) {
    var getMonthlyHistoryService = {};
    return getMonthlyHistoryService;
});

function RandomScaleController($scope, $rootScope, sharedService) {
}

RandomScaleController.$inject = ['$scope', '$rootScope', 'mySharedService'];

and Child module:

var modal = angular.module('modal', ['rootModule', 'ui.bootstrap']);
function MomController($scope, $http, sharedService, getMonthlyHistoryService) {
}
MomController.$inject = ['$scope', '$http', 'mySharedService','getMonthlyHistoryService'];

All works fine, but if i move getMonthlyHistoryService into child module i got Error: Unknown provider: getMonthlyHistoryServiceProvider <- getMonthlyHistoryService.

How I can move getMonthlyHistoryService into child module?

Upvotes: 1

Views: 4984

Answers (1)

Chandermani
Chandermani

Reputation: 42669

How module reference should be created would be to have multiple child modules linked\imported into the parent module

You have done the reverse. You have injected parent module into child module. Now if you move your service from parent to child only this module element can access this service.

Try var myModule = angular.module('rootModule', ['modal']);

and

var modal = angular.module('modal', ['ui.bootstrap']);

Also declare the controller using the module.controller syntax

modal.controller('MomController',['$scope', '$http', 'mySharedService','getMonthlyHistoryService',function($scope, $http, sharedService, getMonthlyHistoryService) {

}]);

Upvotes: 1

Related Questions