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 ...
随机推荐
- Python从入门到精通之环境搭建
本章内容: Windows系统环境搭建 Linux系统环境搭建 Mac OS系统环境搭建 一.下载python安装包 下载地址:https://www.python.org/downloads/ 二. ...
- HTTP协议请求响应模型
HTTP协议请求响应模型:以”用户登录“这个场景来描述 第一步:客户端发起请求到API接口层,操作:用户在客户端填写用户名和密码,点击登录,发送请求. 第二步:api接收到客户端发起的用户请求,api ...
- HDU - 4366 Successor DFS区间+线段树
Successor:http://acm.hdu.edu.cn/showproblem.php?pid=4366 参考:https://blog.csdn.net/colin_27/article/d ...
- webpack多页应用架构系列(一):一步一步解决架构痛点
这系列文章讲什么? 前些时间,写过一个项目,前后端分离,没有借助任何框架,项目页面特别的多,页面都是html直接写的,许多公共html,写了好多处,有一个地方需要改就得改好多地方,js也是随意写,每个 ...
- 【Offer】[67] 【把字符串转换成整数】
题目描述 思路分析 测试用例 Java代码 代码链接 题目描述 将一个字符串转换成一个整数(实现Integer.valueOf(string)的功能,但是string不符合数字要求时返回0),要求不能 ...
- Python中字符编码及转码
python 字符编码及转码 python 默认编码 python 2.X 默认的字符编码是ASCII, 默认的文件编码也是ASCII python 3.X 默认的字符编码是unicode,默认的文件 ...
- Asterisk13.23.1如何增加G723编码和G729编码
文章主要将如何配置Asterisk G729的编码和G723的编码问题 今天在配置语音电话过程中踩到一个坑,就是在对接线路过程中出现了一个报错,在传到对方线路过程中出现无法转码从而导致报错. 查看了下 ...
- Dapper学习(二)之Query相关
0. FIrst , Single & Default 使用这个方法时要小心,First 和 Single 是不同的. 这里,对这个表做下说明: 如果使用 First , 当没有查到元素时,会 ...
- Go操作etcd
etcd是近几年比较火热的一个开源的.分布式的键值对数据存储系统,提供共享配置.服务的注册和发现,本文主要介绍etcd的安装和使用. etcd etcd介绍 etcd是使用Go语言开发的一个开源的.高 ...
- WEB应用中普通java代码如何读取资源文件
首先: 资源文件分两种:后缀.xml文件和.properties文件 .xml文件:当数据之间有联系时用.xml .properties文件:当数据之间没有联系时用.properties 正题: ...
