[Angular 2] Factory Provider with dependencies
This lesson discusses when and how to add dependencies, resolved by Angular’s DI, to factory providers. The example used in this lesson builts upon the previous lesson on understanding factory providers.
For example, the LoggerProvider need to use ConsoleService, so when you put LoggerProvider into 'providers' array, we also need to provide dependence which refer to ConsoleService, the way to do this:
providers: [ TodoService, ConsoleService, ,{ provide: LoggerProvider, useFactory: (consoleService) => { return new LoggerProvider(consoleService, true) }, deps: [ConsoleService] } ],
We add 'deps' prop. The order you write into 'deps' also affect the one you inject to useFactory() function:
providers: [ TodoService, ConsoleService, TranslateService, ,{ provide: LoggerProvider, useFactory: (cs, ts) => { return new LoggerProvider(cs, ts, true) }, deps: [ConsoleService, TranslateService] } ],
'cs' is the instance of 'ConsoleService'; and 'ts' is the instance of 'TranslateService'.