0

I am running into an issue injecting a service from an angular js module into my main controller. I am getting this error: https://docs.angularjs.org/error/$injector/unpr?p0=aProvider%20%3C-%20a%20%3C-%20solrquery

My code is below:

var app = angular.module('app1',[
    'solr'
    ]);

app.controller('app1cont', ['$scope', 'solrquery', function($scope,solrquery){
    console.log("Start");
    $scope.value = 1;
    solrquery('aaa');

}]);

The solr module looks like this:

var solr1 = angular.module('solr', []);

solr1.run(function(){
    console.log("Module Loaded");
});

solr1.service('solrquery',function(a) {
    console.log('searching for: ' + a);
}
);

When I run it, I see Module Loaded printed to the console and an error. If I remove the injection from the app.controller and solrquery('aaa'); the code runs fine... Spent several hours on this already, any help would be much appreciated.

Thanks in advance.

EDIT:

Thanks to Anthony for the answer, after re-working the code, the module looks like this:

var solr1 = angular.module('solr', []);

solr1.run(function(){
    console.log("Module Loaded");
});
//as a factory
solr1.factory('solrquery',function() {
    return function(a) {
        console.log('searching for: ' + a);
    }
}
);
//as a service
solr1.service('solrser', function() { 
    this.test = function(a) {
        console.log(a);
    }
});

And the controller: var app = angular.module('app1',[ 'solr' ]);

app.controller('app1cont', ['$scope', 'solrquery', 'solrser', function($scope,solrquery,solrser){
    console.log("Start");
    $scope.value = 1;
    solrquery('a');
    solrser.test('a');

}]);

2 Answers 2

1

The injector is complaining because it is looking for something called a to inject into the service. You can fix this by changing solrquery to a factory that returns a function...

solr1.factory('solrquery', function () {
    return function (a) {
        console.log('searching for: ' + a);
    };
});

Live Demo

Note that leaving it as a service technically still works, but a function that returns something should really be a factory.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi Anthony, Thanks, I got the factory example working. However, can you tell me how I would add a service from a different module? I am able to DI services that are declared at the app level, just not from the module. Thanks again
0

That angularjs.org error link tells you what the problem is.

Unknown provider: aProvider <- a <- solrquery

Angular's injector is looking for a dependency called a which is specified in the function parameters for the solrquery service:

solr1.service('solrquery',function(a) {

Your service definition needs to be reworked. This is a good reference for how you should be defining services: angular.service vs angular.factory.

1 Comment

I know it needed to be re-worked, I just didn't know how. Stating the obvious without providing remediation steps just clutters the post.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.