MFIhsan
MFIhsan

Reputation: 1057

AngularJS - Error injecting service from a different module

I'm new to AngularJs and I am not able to quite figure out the issue below.

In the example below, there are 2 services MongoRESTService & MongoRESTService2 defined in module common-services.

Main module olive depends on common-services, and the main controller tries to invoke functions in those 2 services above.

Though I was able to call the function MongoRESTService.get() successfully, MongoRESTService.queryMongoData() is throwing below error.

Error Message

angular.js:10126 Error: [$injector:unpr] http://errors.angularjs.org/1.2.28/$injector/unpr?p0=MongoRESTService2Provider%20%3C-%20MongoRESTService2

Unknown provider: MongoRESTService2Provider <- MongoRESTService2

Code

// services.js

var oliveModule = angular.module('common-services')

oliveModule.service('MongoRESTService', function($http, $q, $scope) {

   this.get = function(path){
      ...
   };
});

oliveModule.service('MongoRESTService2', function($scope, MongoRESTService) {
    this.queryMongoData = function (){
        MongoRESTService.get('/clients').then(function(data){
            $scope.clients = ...;
        });
    };
});


// main.js
var oliveModule = angular.module('olive', ['common-services']);

oliveModule .controller('MainController',
    function($scope, $http, MongoRESTService, MongoRESTService2) {
        //this line works
        MongoRESTService.get('/clients').then(function(data){
            ...
        })

        //this throws error
        MongoRESTService2.queryMongoData();
    }
);

Upvotes: 2

Views: 849

Answers (1)

yUdoDis
yUdoDis

Reputation: 1098

It is exactly as the error states,

so try

oliveModule.service('MongoRESTService2',['$scope','MongoRESTService',function($scope, MongoRESTService) {
    this.queryMongoData = function (){
        MongoRESTService.get('/clients').then(function(data){
            $scope.clients = ...;
        });
    };
}]);

Upvotes: 4

Related Questions