Basically, I wanted to make a provider similar to ngResource that could simplify data resolution. I wanted to make it reusable so I put it into its own module.

It had to be a provider so that I could inject it into my main app’s .config() function. Services and factories cannot be injected into .config(). The Angular docs have a helpful table to explain that.

I needed the provider available in .config() so I could define my routes there and use the provider for the resolve objects.

So my provider needs to rely on $http and $route. The problem is that those are not defined at the .config() phase of a module. So I couldn’t define any provider functions that rely on those.

That is where the concept of $get comes in with providers. $get is processed by $injector.invoke() which allows $get to be a function with a dependency. Usually you see an array like this:

So in .config() my provider can’t do anything with $http or $route, but it can queue up tasks that will be handled by $http and $route later. In my case when a route was fired.

So when I write a controller that relies on ApiProvider I access it as Api instead of ApiProvider. And the controller will receive the object returned by this.$get() so the provider is guaranteed to have those dependencies by then.

Categories

Meta

Ken Snyder is a Software Craftsman specializing in PHP and JavaScript. Ken works in Salt Lake city for Right Intel.
He is the co-founder of UtahJS, an educational non-profit aimed at promoting JavaScript in Utah.