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. LoadRunner利用ODBC编写MySql脚本(转)

    LoadRunner测试数据库是模拟客户端去连接数据库服务器,因此,需要协议(或者说驱动的支持).LoadRunner本身直接支持Oracle.SqlServer数据库,这两个数据库直接选择相应的协议 ...

  2. MongoDB 日志太大怎么办?

    MongoDB的日志增长的非常快,/var所在的空间立即就占满了,即便换到还有一个磁盘分区保存日志.日志还是增长的非常快.磁盘眼看要告磬. 有一个好办法,就是使用旋转日志. MongoDB的旋转日志有 ...

  3. jquery 获取html <img /> 位置时出错问题

    如图所示,这样端口小图片都是通过jquery html()方法设置的(参数html就是画整个图片的html字符串),如图: 但是出现图片没有完全渲染完的问题,如图: 从图中可以看出在代码运行到断点的时 ...

  4. cmd启动JMeter

    <配置cmd当前变量启动JMeter> 前因: 不想配置当前windows的环境变量,减少配置污染,故自己写一个启动脚本.每次启动直接双击就可以了. 启动脚本和目录结构是下面这样的: 脚本 ...

  5. Java类载入器 ClassLoader的解析

    //參考 : http://www.ibm.com/developerworks/cn/java/j-lo-classloader/ 类载入器基本概念 类载入器是 Java 语言的一个创新,也是 Ja ...

  6. 关于 AngularJS 的数据绑定

    单向绑定(ng-bind) 和 双向绑定(ng-model) 的区别 ng-bind 单向数据绑定($scope -> view),用于数据显示,简写形式是 {{}}. 1 <span n ...

  7. C数据结构-栈和队列,括号匹配举例---ShinePans

    1.栈和队列是两种特殊的线性表             运算操作被限定仅仅能在表的一端或两端插入,删除元素,故也称它们为限定的线性表结构 2.栈的基本运算 1).Stackinit(&s) 构 ...

  8. xUtils工具实现下载功能

    private String download_url="http://192.168.2.8:80/DownZip/*****.zip";//下载的路径 public  Stri ...

  9. shell判断文件夹是否存在

    #shell判断文件夹是否存在 #如果文件夹不存在,创建文件夹 if [ ! -d "/myfolder" ]; then mkdir /myfolder fi #shell判断文 ...

  10. lucene4.7学习总结 (zhuan)

    http://blog.csdn.NET/mdcmy/article/details/38167955?utm_source=tuicool&utm_medium=referral ***** ...