contentchild

// 使用方法
git clone https://git.oschina.net/mumu-osc/learn-component.git
cd learn-component
git pull origin contentchild
npm install
ng serve
<!-- test-content-child.component.html -->
<div class="panel panel-primary">
<div class="panel-heading">父组件</div>
<div class="panel-body">
<child-one>
<h3>投影的标题</h3>
<p>投影的底部</p>
<child-two></child-two>
<child-two></child-two>
<child-two></child-two>
<child-two></child-two>
<child-two></child-two>
<child-two></child-two>
<child-two></child-two>
<child-two></child-two>
</child-one>
</div>
</div>
<!-- child-one.component.html -->
<div class="panel panel-primary">
<div class="panel-heading">
<ng-content select="h3"></ng-content>
</div>
<div class="panel-body">
<ng-content select="child-two"></ng-content>
</div>
<div class="panel-footer">
<ng-content select="p"></ng-content>
</div>
</div>
// child-one.component.ts
import { Component, ContentChild, ContentChildren, ElementRef, OnInit, QueryList } from '@angular/core';
import { ChildTwoComponent } from '../child-two/child-two.component'; @Component({
selector: 'child-one',
templateUrl: './child-one.component.html',
styleUrls: ['./child-one.component.scss']
})
export class ChildOneComponent implements OnInit {
// @ContentChild(ChildTwoComponent)
// childTwo:ChildTwoComponent;
@ContentChildren(ChildTwoComponent) childrenTwo:QueryList<ChildTwoComponent>;
constructor() { } ngOnInit() {
} ngAfterContentInit():void{
// console.log(this.childTwo);
this.childrenTwo.forEach((item)=>{
console.log(item);
});
}
}

什么是ContentChild

contentchild与viewchild作用非常相似,区别在于contentchild比viewchild多了一个布局功能

比如以上代码中的p标签的内容,就显示在child-one组件的最底部

Angular ContentChild的更多相关文章

  1. [Angular] @ContentChild with Directive ref

    For example you have a component, which take a trasclude input element: <au-fa-input id="pas ...

  2. [Angular] @ContentChild and ngAfterContentInit

    @ContentChild normally works with ngAfterContentInit lifecycle. @ContentChild is used for looking in ...

  3. [Angular] Write Compound Components with Angular’s ContentChild

    Allow the user to control the view of the toggle component. Break the toggle component up into multi ...

  4. [Angular] Difference between ViewChild and ContentChild

    *The children element which are located inside of its template of a component are called *view child ...

  5. angular ViewChild ContentChild 系列的查询参数

    官方说明 官方文档 在调用 NgAfterViewInit 回调函数之前就会设置这些视图查询. 元数据属性: selector - 用于查询的指令类型或名字. read - 从查询到的元素中读取另一个 ...

  6. Angular 向组件传递模板的几种方法

    最近在写一个日期选择器组件,为了满足将来可能出现的各种需求,所以需要能够高度的自定义组件的样式.为了达到这个目的,需要能够在日期选择器组件外控制每个日期格子内要显示的内容,比如,标上节假日之类的.这时 ...

  7. Angular:利用内容投射向组件输入ngForOf模板

    现在,我们写一个组件puppiesListCmp,用于显示小狗狗的列表: //puppies-list.component.ts @Component({ selector: 'puppies-lis ...

  8. Angular开发实践(三):剖析Angular Component

    Web Component 在介绍Angular Component之前,我们先简单了解下W3C Web Components 定义 W3C为统一组件化标准方式,提出Web Component的标准. ...

  9. Angular开发实践(八): 使用ng-content进行组件内容投射

    在Angular中,组件属于特殊的指令,它的特殊之处在于它有自己的模板(html)和样式(css).因此使用组件可以使我们的代码具有强解耦.可复用.易扩展等特性.通常的组件定义如下: demo.com ...

随机推荐

  1. openvpn mac客户端tunnelblick连接后自动添加路由

    在openvpn 的服务器配置文件添加配置设置客户端连接后自动添加一条路由 示例: vim /etc/openvpn/server.conf # Push routes to the client t ...

  2. WebService注解

    package cn.itcast.service; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.W ...

  3. 如何在 Android 手机上安装 Ubuntu 13.04

    在农村,一切都是相对的. 长话短说,我已经厌倦了我们在农村做的一个小小的突破. 我本该去放松,读了一大堆东西涉及到自然语言的处理.但是因为我这些天一直在修改LXC和chroot的时候,我决定将我的An ...

  4. pycharm 控制台字体背景颜色设定让你美到爆

    还在为你控制台字体颜色不入眼而发愁吗? 在某些特殊时刻想要改变下字体的颜色却无从下手而发愁吗? 那你就进来看一看,瞧一瞧,之需看2分钟,花2分钟理解,花3分钟练练手就能让你的控制能字体炫酷起来! 一. ...

  5. 摘:常用函数(包括:宽字符函数、普通C函数 )

    只要看见“W”就是宽的意思,左边wchar_t,右边char 字符分类: 宽字符函数            普通C函数            描述 iswalnum()             isa ...

  6. mysql 插入 详解

    表创建好后,就可以往里插入记录了,插入记录的基本语法如下: INSERT INTO tablename (field1,field2,……fieldn) VALUES(value1,value2,…… ...

  7. 修改host简化远程访问

    问题描述: 使用本机登陆服务器时,需要经常输入IP地址,在局域网下和非局域网下输入的IP又不一样,十分麻烦,如果可以给IP命名一个简单的名字,岂不是很方便? 解决方法: 修改host文件: vim / ...

  8. unity5, 在mac下多开

    mac上app的多开与app本身无关,而是系统本身的功能,使用命令 open -n 就可以实现打开某应用程序的一个新实例(自行输入man open查看含义). 参考:http://mac-how-to ...

  9. 使用JSTL的sql:query标签制作分页查询遇到NoSuchFieldError: deferredExpression

    参考:http://hi.baidu.com/desyle/item/4fe650265792d7182a0f1c33 症状: 如题所述,代码如下 <sql:query var="re ...

  10. 浏览器 本地预览图片 window.url.createobjecturl

    第一种方式 <script type="text/javascript"> function setImagePreview() { var docObj = docu ...