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

  • 相关阅读:
    phpajax高级篇
    一天学会ajax (php环境)
    php生成静态文件的方法
    MongoDB查询文档
    MongoDB删除文档
    MongoDB索引管理
    MongoDB插入文档
    MongoDB排序记录
    MongoDB 更新文档
    mongoDB 固定集合(capped collection)
  • 原文地址:https://www.cnblogs.com/Answer1215/p/5877840.html
Copyright © 2011-2022 走看看