Angular Js Resolve Dependencies Of A Function

1 minute read

The requirement is to invoke a function in an angular app but also wanted the depencies of the function (parameters) such as servies and factories to be injected automatically.

function CustomFunction(MyAngularService) {

When the function is invoked, the service should be injected automatically and also I should be able to pass additional parameters which are not injectables.

As it turns out, its really an easy task with the $injector service.


Doing so will provide all the depencies required for the function and invoke it. Pretty awesome. It’s also worth noting that there is an instantiate method as well in $injector service which will create a new instance out of the provided constructor function.

Passing Custom Parameters

As I mentioned above I also wanted to pass additional parameters to the function in addition to the injectables like below.

function CustomFunction(MyAngularService, name) {

name is a custom parameter that I should provide.

This is how its done.

$injector.invoke(CustomFunction, this, { name: 'Raathi' });

The third paramter of the invoke method is an object where you can specify your custom parameters. The $injector will go through the third object to see if a particular parameter is present. If its not present only, it will check the angular app for such injectable.

Here is the stackoverflow question I created to help with this issue.

Happy Coding People!

Leave a Comment