container Component:

<app-host-decorator appHostDecorator><app-host-decorator-child></app-host-decorator-child>
</app-host-decorator>

app-host-decorator的实现:

import {Component} from '@angular/core';
import {HostComponentService} from './host-component.service';
import {TOKEN_HOST_CLASS_PROVIDER} from '../parameter-decorator-constant';
import {HostTokenComponentService} from './host-token-component.service';@Component({selector: 'app-host-decorator',template: `<h3>@Host -- 获取宿主元素注入器里面注入的对象</h3><ng-content></ng-content>`,providers: [HostComponentService,{provide: TOKEN_HOST_CLASS_PROVIDER, useClass: HostTokenComponentService}]
})
export class HostDecoratorComponent {}

app-host-decorator-child的实现,该child Component的template内容,会显示在parent Component的ng-content里:

import {Component, Host, Inject} from '@angular/core';
import {HostComponentService} from './host-component.service';
import {TOKEN_HOST_CLASS_PROVIDER} from '../parameter-decorator-constant';
import {HostTokenComponentService} from './host-token-component.service';@Component({selector: 'app-host-decorator-child',template: `<p>child Component里,ng-content对应的内容</p>`
})
export class HostDecoratorChildComponent {constructor(@Host() private componentService: HostComponentService,@Host() @Inject(TOKEN_HOST_CLASS_PROVIDER) private tokenService: HostTokenComponentService) {}
}

消费该Component的HTML代码:

<app-host-decorator appHostDecorator><!--<app-host-decorator-child></app-host-decorator-child>-->
</app-host-decorator>

Directive的代码:

import {Directive, Host, Inject} from '@angular/core';
import {HostComponentService} from './host-component.service';
import {TOKEN_HOST_CLASS_PROVIDER} from '../parameter-decorator-constant';
import {HostTokenComponentService} from './host-token-component.service';@Directive({selector: '[appHostDecorator]'
})
export class HostDecoratorDirective {/*** @Host() 获取宿主元素里面提供的服务(宿主元素注入器提供的服务)* @param componentService* @param tokenService*/constructor(@Host() private componentService: HostComponentService,@Host() @Inject(TOKEN_HOST_CLASS_PROVIDER) private tokenService: HostTokenComponentService) {console.log('in host Decorator directive: ',componentService, tokenService);}
}

运行时我们能发现,无论是在Directive里还是在child Component里,我们使用@host,都能拿到 host Component里注入器注入的实例:

更多Jerry的原创文章,尽在:“汪子熙”:

使用@host获得宿主元素注入器里注入的内容相关推荐

  1. Mac系统下docker容器无法使用--net host共享宿主机端口的解决方案

    文章目录 背景 解决过程一 分析 Mac系统下docker容器无法使用--net host共享宿主机端口的原因 docker的网络配置 host模式 overlay模式 macvlan模式 bridg ...

  2. 投影元素直接隔离_Angular ngcontent 内容投影

    前言 内容投影和ng-content是可以让我们最大程度构建可重用组件的Angular功能之一.我们来构造一个小组件,一个Font Awesomne输入框.我们设计这个组件的目标是为了构造一个带有图标 ...

  3. python txt提取特定数据_Python提取列表中的内容 用“python”怎么提取文件里的指定内容?...

    用"python"怎么提取文件里的指定内容? python读取文件内容的方法: 一.最方便的方法是一次性读取文件中的所有内容并放置到一个大字符串中: all_the_text = ...

  4. cvc-complex-type.2.4.d: 发现了以元素 ‘base-extension‘ 开头的无效内容。此处不应含有子元素。

    问题描述 升级到了最新的AS版本之后,原来还能跑的项目现在跑不了了,报: cvc-complex-type.2.4.d: 发现了以元素 'base-extension' 开头的无效内容.此处不应含有子 ...

  5. 同步项目时,出现cvc-complex-type.2.4.a: 发现了以元素xxxxxx开头的无效内容。应以 xxxxx 之一开头的问题解决方案

    前言 大家在接手老项目的时候会不会出现如下述问题呢. cvc-complex-type.2.4.a: 发现了以元素 'base-extension' 开头的无效内容.应以 '{layoutlib}' ...

  6. js中显示一个指定html文档,JS实现选定指定HTML元素对象中指定文本内容功能示例...

    本文实例讲述了JS实现选定指定HTML元素对象中指定文本内容功能.分享给大家供大家参考,具体如下: 该功能用处多多,可以灵活运用之!主要函数如下: //选中文本中指定部分 function selec ...

  7. html粘贴excel内容,如何用JS解析剪切板里的excel内容

    前言 这次记录的是昨晚一个想法:把excel内容复制到剪切板并转成自己想要的json格式,核心是要把excel内容转json,这部分主要看excel的格式和json如何业务的映射,不展开.倒是通过实践 ...

  8. MyBatis中提示:元素类型为 mapper 的内容必须匹配 (cache-ref|cache|resultMap*|parameterMap*|sql*|insert*|update*|de

    场景 整合MyBatis后运行项目提示: 元素类型为 "mapper" 的内容必须匹配 "(cache-ref|cache|resultMap*|parameterMap ...

  9. 元素类型为 “resultMap” 的内容必须匹配 “(constructor?,id*,result*,association*,collection*,discriminator?)”

    错误 原配置如下: <resultMap id="xxxDetail" type="xxx" extends="xxxResult"& ...

最新文章

  1. LLYFSpy W.I.P
  2. miniui datagrid 隐藏列默认赋值_「小程序JAVA实战」 小程序默认加载的页面和生命周期(八)...
  3. 【转】SAP S4 OP/Cloud 10个区别
  4. 如何查找历史线程阻塞原因_学习 Web Worker(js中的“多线程”)
  5. 网卡驱动:stmmac DMA接收流程
  6. 【TensorFlow】——Windows10、pycharm、Python3.6.4中安装CPU和GPU两种版本的TensorFlow
  7. python windows窗口置顶_想用Python编程却不知如何下手?一篇搞定编程准备工作
  8. 计算机兴趣小组活动教学教案,长春一中计算机兴趣小组教案.ppt
  9. SP1437 Longest path in a tree(树的直径)
  10. 一个java错误的总结
  11. 思维转换感悟与区块链视频资料分享
  12. 为什么C语言执行效率高,运行快?
  13. 立体声音频效果插件:TBProAudio ST1 for Mac
  14. 自适应Simpson
  15. cypress离线安装_新思、敦泰、汇顶/赛普拉斯触控ICD万能脱机烧录测试工具TP-TEST...
  16. 刚刚下载的eclipse打不开?一点击就报错
  17. unity画一条流动的弧线(贝塞尔线)
  18. 晶圆测试厂wafer map管理建议
  19. MediaPlayer代码分析(1)-初始化和设置数据的过程
  20. 计算机网络——IP数据报的发送和转发过程

热门文章

  1. iOS中UIWebview中网页宽度自适应的问题
  2. Spring源码解析:自定义标签的解析过程
  3. Android知识点剖析系列:深入了解layout_weight属性
  4. 第四章 Tomcat服务器的安装及配置2
  5. JSF 2.2: HTML5 Support
  6. mvn tutorial
  7. php支持gd,png,jpeg,zlib
  8. 手把手教你安装VMware虚拟机
  9. 方立勋_30天掌握JavaWeb_div和css基础
  10. VC windows 多网卡情况下 获取当前网卡ip地址