Define and run an arbitrary function that can have Angular services injected into it.
Versions used below
Why not just use closures?
The easy solution is using a closure to access dependencies instead of injecting them as parameters.
In my case, the function was created in a module config block and passed to a provider to be invoked later. You can only inject providers and Angular constants into config blocks so the closure trick won’t work.
The above results in error:
Angular makes this pretty easy. Use $injector to invoke the function.
func with all its dependencies as parameters and returns
func’s return value.
func must follow one of the three Angular “injectable function” patterns:
Note: this style won’t work if the code is minified
This injection can happen anywhere. It’s not in the Angular API like all other injections we’re used to. Will ng-annotate still see it?
The answer is… maybe.
There is only so much ng-annotate can detect through static code analysis. It has options for explicitly marking code to be annotated but at that point you might as well just annotate the code yourself.