Angular template ng-template/container/content
1. ng-template
- 形式:
<ng-template>...</ng-template> - 默认ng-template中的内容会隐藏;
- 可通过[ngIf]来控制内容显示隐藏;
- 此标签不会影响原本html结构;
html:
<ng-template [ngIf]="true">
this is template!
</ng-template>
浏览器输出:

浏览器调试窗口

2. template
- 形式:
<template>...</template> - 默认内容会隐藏;
- 可通过Css样式display来控制内容显示隐藏;
- 此标签会影响原本html结构;
html:
<template style="display: block;">
block;
</template>
浏览器输出:

浏览器调试窗口:

3. ng-container
- 形式:
<ng-container>...</ng-container> - 默认内容显示;
- 可通过*ngIf来控制内容显示隐藏;
- 此标签不会影响原本html结构;
html:
<ng-container>
this is container!
</ng-container>
浏览器输出:

浏览器调试窗口:

4. ng-content
- 形式:
<ng-content select = 'DOM标签/class类/id/属性等'>...</ng-content> - 用于内容映射,可以定制可复用组件;
- 引用此组件时,selector标签中间的内容将投射(或者说插入)到此组件的ng-content中;
- 此标签上有一个select属性,查找可以与select值相符合的内容,映射到此处;它的值可以为别的组件的selector、html标签、class类或ID等;
(1). 无select属性情况下的代码:
// 子组件
@Component({
selector: 'app-child',
template: `<ng-content></ng-content>`
})
// 父组件
@Component({
selector: 'app-parent',
template: `
<app-child>内容映射1</app-child>
<app-child>内容映射2</app-child>`
})
浏览器输出:
浏览器调试窗口:

(2). 有select属性情况下的代码:
// content-component.html
<div>
<ng-content select="h3.title"></ng-content>
<ng-content select="p.intro"></ng-content>
<div class="content-cmp">
<ng-content select="app-extra"></ng-content>
</div>
</div>
// parent-component.html
<app-content>
<p class='intro'>段落</p>
<h3 class='title'>标题</h3>
<app-extra></app-extra>
</app-content>
浏览器输出:

4-1. 获取 <ng-content></ng-content>映射的内容
- ContentChild - 获取单个实例
- ContentChildren - 以QueryList 形式返回多个实例
// content.component.ts
@ContentChild(ExtraComponent) extraCmp: ExtraComponent;
// 获取到之后可以在ngAfterContentInit()方法中操作extraCmp组件实例
Angular template ng-template/container/content的更多相关文章
- Package template (html/template) ... Types HTML, JS, URL, and others from content.go can carry safe content that is exempted from escaping. ... (*Template) Funcs ..
https://godoc.org/text/template GoDoc Home About Go: text/templateIndex | Examples | Files | Directo ...
- Django.template框架 template context (非常详细)
前面的章节我们看到如何在视图中返回HTML,但是HTML是硬编码在Python代码中的 这会导致几个问题: 1,显然,任何页面的改动会牵扯到Python代码的改动 网站的设计改动会比Python代码改 ...
- Error resolving template [xxx], template might not exist or might not be exist
Springboot+thymeleaf+mybatis 抛Error resolving template [xxx], template might not exist的异常 原因是我们在pom. ...
- Error resolving template: template might not exist or might not be accessible是一句缩水报错?
一 thymeleaf在开发的时候本地调试正常,但是在测试环境打成jar包就报这个错误了. 二 template might not exist or might not be accessible ...
- 【报错】An error happened during template parsing (template: "class path resource [templates/adminManageCourse.html]")
页面显示: Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing t ...
- 【报错】An error happened during template parsing (template: "class path resource [templates/hello1.html]")
页面显示: Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing t ...
- Thymeleaf 异常:Exception processing template "index": An error happened during template parsing (template: "class path resource [templates/index.html]")
Spring Boot 项目,在 Spring Tool Suite 4, Version: 4.4.0.RELEASE 运行没有问题,将项目中的静态资源和页面复制到 IDEA 的项目中,除了 IDE ...
- [Angular 2] Share Template Content In Another Template With Content Projection <ng-content>
Angular 1 provided a mechanism to place content from your template inside of another template called ...
- [Angular 2] Create template with Params
Angular 2 templates have a special let syntax that allows you to define and pass a context when they ...
随机推荐
- 小白专场-多项式乘法与加法运算-c语言实现
目录 一.题意理解 二.求解思路 三.多项式的表示 3.1 数组 3.2 链表 四.程序框架搭建 五.如何读入多项式 六.如何将两个多项式相加 七.如何将两个多项式相乘 八.如何将多项式输出 一.题意 ...
- Gym 100851 Distance on Triangulation
题意:给你一个N边形, 然后这个n边形有n-3条边,然后询问2点之间的最短路. 题解:分治. 我们可以找到一条边,使得这幅图能分成大小相同的2幅图,那么我们就可以确定那些被分割开的询问的答案是多少了. ...
- 深入浅出TypeScript(4)- 使用接口和类型别名
在TypeScript中,为了可以约束对象定义,提供了两个新的特性,接口和类型别名. TypeScript中的接口 在强类型语言中,都有接口的概念,那么TypeScript中的接口是如何使用的呢? 接 ...
- PHP的调试环境程序集成包----phpStudy
PHP (超文本预处理器) PHP即“超文本预处理器”,是一种通用开源脚本语言.PHP是在服务器端执行的脚本语言,与C语言类似,是常用的网站编程语言.PHP独特的语法混合了C.Java.Perl以及 ...
- idea中applicationContext-trans.xml中的Cannot resolve bean 'dataSource'...的问题解决
问题如下: (applicationContext-trans.xml中的部分截图) 先了解问题是怎么出现的: 此处的dataSource是在applicationContext-dao.xml中配置 ...
- Python中使用pip安装库时提示:远程主机强迫关闭了一个现有的连接
场景 在cmd中使用pip install moviepy时,需要安装一些依赖库,很长时间后提示: 远程主机中断了一个现有的连接. 原因是默认镜像源下载过慢,将其修改为国内或者设置安装时的源. 这里以 ...
- 大数据平台搭建 - cdh5.11.1 - spark源码编译及集群搭建
一.spark简介 Apache Spark 是专为大规模数据处理而设计的快速通用的计算引擎,Spark 是一种与 hadoop 相似的开源集群计算环境,但是两者之间还存在一些不同之处,这些有用的不同 ...
- Sticks(剪枝+BFS)
Problem Description George took sticks of the same length and cut them randomly until all parts beca ...
- 05:(H5*) node、npm、nrm
目录: 1:NPM 2:CNPM 3:NRM 4:homebrew 5:具体指令 6: -g -S -D 1:NPM NPM的全称是Node Package Manager, 是一个NodeJS包管理 ...
- EasyJson 发布
JSON库很常用了,现在开源的JSON库也有很多.但是我们仍然面临下列问题 1)时不时的爆出这个Json库出现漏洞,那个json库出现漏洞.一旦出现漏洞只能升级,想切换JSON都不成. 2)一个项目中 ...
