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. linux动态链接库导出函数控制

    windows 环境的vc的话,可以方便的指定__declspec(dllexport) 关键字来控制是否把dll中的函数导出.我也来测试一下linux下面是如何做的:先看gcc 和ld的相关选项 = ...

  2. C++ bool和string转换

    直接贴代码吧.用g++能够编译.測试ok #include <iostream> #include <sstream> using namespace std; int mai ...

  3. java调试打断点和不打断点执行结果不一致问题解决

    java程序在调试的时候需要debug来跟踪一下结果,有一种情况是这样的,正常执行java程序就会出现问题,而断点debug跟踪此方法的时候却是正常的,不断测试结果都是这样,由此判断有可能是因为此方法 ...

  4. layui数据表格自定义每页条数limit

    table.render({ elem: '#data_grid' //,width: 900 //,height: 274 ,cols: [[ //标题栏 {field: 'id', title: ...

  5. centos x64 vsftpd 530登陆错误问题

    近来在centos 6.0 x64版本下安装vsftpd,中间出现一些问题,解决过程总结如下: 安装vsftpd:yum install vsftpd 安装后配置为虚拟用户登陆,然后用:ftp loc ...

  6. android.graphics(1) - Paint, Canvas, drawLine, drawPoint, drawRect, drawRoundRect, drawCircle, drawOval, drawArc

    一.Paint与Canvas 像我们平时画图一样,需要两个工具,纸和笔.Paint就是相当于笔,而Canvas就是纸,这里叫画布. 所以,凡有跟要要画的东西的设置相关的,比如大小,粗细,画笔颜色,透明 ...

  7. Getting Started with Amazon EC2 (1 year free AWS VPS web hosting)

    from: http://blog.coolaj86.com/articles/getting-started-with-amazon-ec2-1-year-free-aws-vps-web-host ...

  8. python操作word

    python教程(百度经验) Python 操作Word(Excel.PPT等通用)   import win32comfrom win32com.client import Dispatch, co ...

  9. 基于js白色简洁样式计算器

    今天给大家分享一款白色简洁样式计算器JS代码是一款精美简洁计算器JS代码插件网页特效,软件应用,后台应用JS计算器插件代码免费下载.适用浏览器:360.FireFox.Chrome.Safari.Op ...

  10. modSecurity和Naxsi哪个更适合Nginx搭建WAF

    nginx增加modsecurity模块 modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成 ...