A factory provider in Angular 2 often has its own dependencies to construct an object. In our list component, we have a factory provider that creates an instance of logDebugger without the need of additional dependencies, except the Boolean value that we can add straight to the constructor.
Let's say we want to swap out this console.log call in log debugger with a reusable service, so we don't have to make changes if needed at several different places in our application where this function is called.
We add a new file in source app, and call it console.service.ts, in which we create a service class, consoleService. It has a method, log, that simply takes a message, and calls console.log with it.
Next, we go back to our logDebugger and import consoleService. Now, we want to make it a dependency of logDebugger. We add a new constructor parameter, consoleService, of type consoleService, and make sure it's used in the debug method instead.
We now go back to our list component, and import consoleService here as well. Next, we add a provider for consoleService. If we take a look at the factory provider for logDebugger now, we realize that the call side of the logDebugger constructor doesn't match the constructor's signature anymore, because logDebugger now asks for a consoleService dependency.
To get an instance of consoleService in the factory function, we can add property, deps, to the provider configuration. deps is a list of provider tokens that map to the dependencies we want to inject. In our case, we want to inject an instance of consoleService.
All we have to do is to add the token for consoleService. Now, the cool thing is that all dependencies which are declared through their tokens on the deps property will now be injected into our factory function in the same order.
In other words, since we have added the consoleService token to deps, we now get an instance of consoleService, which we can then pass to the logDebugger construction accordingly. We save the file, and see that we get the same output as before, but now consoleService is used to log the message.
To wrap it up, we add a provider for a consoleService, so it can be constructed when we ask for it here in the deps property for our factory function, which then gets the actual instance injected, so we can pass it to our logDebugger's constructor.