2

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();
    }
);

1 Answer 1

4

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 = ...;
        });
    };
}]);
Sign up to request clarification or add additional context in comments.

1 Comment

if you need clarification, this is known as the 'array notation' of dependency injection .. docs.angularjs.org/guide/di

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.