0

My question is an extension of asked question link is given below

can't get service instance from $injector.get()

var app = angular.module('myDI', []);
app.config(function($provide){
    $provide.provider('greeting',function(){
        this.$get = function(){
             return function(name) {
                 console.log("Hello, " + name);
            };
        };
    });
});

we can get Service instance using below code.

var injector = angular.injector(['myDI', 'ng']); //Add this line
var greeting = injector.get('greeting');
greeting('Ford Prefect');
var injector = angular.injector();

But if I add dependencies in Service like...

var app = angular.module('myDI', []);
    app.config(function($provide){
        $provide.provider('greeting',['$http','$q' function($http,$q){
            this.$get = function(){
                 return function(name) {
                     console.log("Hello, " + name);
                };
            };
        });
    });

how we will get an instance of service using the angular injector.

Thanks a lot.

Rohit Sharma
  • 3,304
  • 2
  • 19
  • 34
user3460330
  • 165
  • 3
  • 11

1 Answers1

0

Many things in here:

  • you can't inject services at provider definition level. They depend only on what is defined with angular.const and angular.config (and maybe something more, I don't remember anymore). So you can't inject $http or $q this way.
  • if you want to inject services in yoru service, you must inject them at the factory definition level (the $get)
  • to make it simplier don't define your provider in your config
  • $http or $q are not in the default module. To access these services, you must import the 'ng' module that contains it.

Here is the corrected, on which I added a way to configure your provider. you can access it on this plunker: http://jsbin.com/yajomecoxi/edit?js,console

var app = angular.module('myDI', ['ng']);

app.provider('greeting',function(){
  var greetingToUser="Hello, ";
  this.setGreeting=function(greet){
    greetingToUser=greet;
  }
  this.$get = ['$http', '$q', function($http, $q){
    return function(name) {
      console.log(greetingToUser + name);
    };
  }];
});


app.config(['greetingProvider',function(greetingProvider){
  greetingProvider.setGreeting("welcome to you ");
}])


var $injector = angular.injector(['myDI']);
var greeting = $injector.get('greeting');
greeting("sfgsdfg");

see more information on these links:

Stephane
  • 1,359
  • 1
  • 15
  • 27
  • You are right it was factory level not provider.Please tell me how can i access instnace of service. – user3460330 Sep 05 '18 at 14:58
  • everything is in the working sample I gave!!! Moreover you have to full working example on plunker. Basically, you create the injector by mentioning modules where candidate injections will be scanned, and you make a get on it... basically like you did. One more thing, there was couple of js syntax mistakes in your code, I corrected them, but I believe it was just because you quickly wrote it. – Stephane Sep 05 '18 at 15:03
  • I found the solution here... https://stackoverflow.com/questions/17319784/call-angularjs-service-from-simple-js-code – user3460330 Sep 06 '18 at 07:06