码迷,mamicode.com
首页 > 其他好文 > 详细

[Angular 2] 7. Factory Provider with dependencies

时间:2016-09-17 07:07:11      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:

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‘.

[Angular 2] 7. Factory Provider with dependencies

标签:

原文地址:http://www.cnblogs.com/Answer1215/p/5877840.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!