zoukankan      html  css  js  c++  java
  • [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'.

  • 相关阅读:
    第五章 条件语句
    第四章 javaScript运算符
    第三章 javaScript数据类型
    看电影学英语十
    英语口语会话十
    看电影学英语九
    英语口语会话九
    英语口语会话八
    看电影学英语八
    Linux command line and shell scripting buble
  • 原文地址:https://www.cnblogs.com/Answer1215/p/5877840.html
Copyright © 2011-2022 走看看