Skip to content Skip to sidebar Skip to footer

How To Inject Dependencies For Standalone Functions

What is the preferred way to inject dependencies for standalone functions, not services / controllers? There are two cases. The first one is $httpProvider.interceptors.push(functio

Solution 1:

So you are doing totally right for injecting stand alone functions. And if I got the question you are asking why is that works, if you are passing different type instead of expected. It is expect a function , but you are putting an array. I assume that happened because angular mechanism called injector provides all instances of entities you are using in your application, and it is going through all things which is going to be injected, and convert recognized dependencies to their instances. So if it will see such three parameters in function ['$q', '$timeout', '$httpHey', function($q, $timeout, $httpHey) { /* ... */ }];, it will returns you error before app will be initialized, because injector can not find registered service which can be used to create an instance of such service. But if all array items will be recognized, it will create new Instances if there no one exist and pass them to the function as an arguments, otherwise it will take already existing instances of them (because services are singletons).

It is kind of low level stuff of angular, so if you need more extended response, leave comment.

Post a Comment for "How To Inject Dependencies For Standalone Functions"