0

I have this controller that is defined on the body element:

app.controller('bodyController', ['$injection1', '$injection2',
    function($injection1, $injection2){
        // doing stuff here
}]);

Now somewhere else, I want to be able to inject another dependency into this controller. Pseudo-code:

app.controller('bodyController').inject(['$injection3', function($injection3){
    // doming more stuff here
});

Is this possible at all?

1 Answer 1

3

You could inject $injector dependency inside controller & then inside a controller you can directly ask for other dependency in it.

app.controller('bodyController', ['$injection1', '$injection2', '$injector',
    function($injection1, $injection2, $injector){
      var injectorGet = $injector.get;
      //myService will be singleton instance of myService
      var myService = injectorGet('myService');
      //and injectorGet which you can get other dependency too.
}]);
Sign up to request clarification or add additional context in comments.

5 Comments

OP says somewhere else. I think this can't be then answer. Because in this case why not simply injecting myService in the first place?
@SaeedNeamati what I understood from question is, OP wants to add/access 3rd(other) dependency, which he wanted to add later(this is what I assumed), let see what OP wants.. he will comment on it.. correct me if I'm missing anything. Though thanks for heads up.. :)
@PankajParkar, What do I do?I mean injection service elsewhere?
I mean injection service elsewhere? Why don't be injected at first?
@SSH because OP wanted to get dependency object dynamically, there might be a case with OP, where sometimes he might have added service/factory in app module, based on some condition in controller using $injector dependency (The reason I haven't added in DI array is, it will throw an error when dependency isn't there module)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.