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

  • 相关阅读:
    java web 资源文件读取
    页面跳转
    验证码的随机图片
    spring 注解
    回文字符串系列问题
    【leetcode】Find All Anagrams in a String
    斐波那契数列
    【leetcode】 First Missing Positive
    Trapping Rain Water
    区间合并问题
  • 原文地址:https://www.cnblogs.com/Answer1215/p/5877840.html
Copyright © 2011-2022 走看看