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 ...
随机推荐
- 牛客 136G-指纹锁 set容器重载
136G-指纹锁 题意: 设计一个容器,支持插入x,若与容器中的值最小相差为k,则自动忽略.删除操作,把与x相差为k的值都从容器中删除.查询操作,问容器中有没有和x相差为k的数值. 思路: 一个stl ...
- ACM-ICPC 2018 徐州赛区(网络赛)
目录 A. Hard to prepare B.BE, GE or NE F.Features Track G.Trace H.Ryuji doesn't want to study I.Charac ...
- WordCount--统计输入文件的字符数、行数、单词数(java)--初级功能
码云地址: https://gitee.com/YuRenDaZ/WordCount 个人PSP表格: PSP2.1 PSP阶段 预估耗时 (分钟) 实际耗时 (分钟) Planning 计划 180 ...
- 【Offer】[56-2] 【数组中唯一只出现一次的数字】
题目描述 思路分析 测试用例 Java代码 代码链接 题目描述 在一个数组中除一个数字只出现一次之外,其他数字都出现了三次.请找出那个只出现一次的数字 [牛客网刷题地址]无 思路分析 如果一个数字出现 ...
- 05 python内置函数大全
一.数学运算 abs:返回数字的绝对值 abs(-1) round:保留浮点数的小数位数,默认保留整数.四舍五入. round(1.553,1) #1.6 divmod:计算除数和被除数的结果,并返回 ...
- ECMAScript es6新功能讲解视频教程
下载链接:https://www.yinxiangit.com/1.html 目录: 01.课程介绍-ECMAScript 新功能.mp402.块的作用域-let.mp403.恒量-const.mp4 ...
- QRowTable表格控件(五)-重写表头排序、支持第三次单击恢复默认排序
目录 一.原生表格 二.效果展示 三.实现方式 1.排序列定制 2.排序交互修改 四.相关文章 原文链接:QRowTable表格控件(五)-重写表头排序.支持第三次单击恢复默认排序 一.原生表格 开发 ...
- 使用openlivewriter编写cnblogs博客
下载OpenLiveWriter 下载地址:http://openlivewriter.org/ 安装OpenLiveWriter 1.账号配置 2.常规操作,省略- 安装高亮插件 1.下载插件:ht ...
- innobackupex备份参数slave-info、safe-slave-backup
mysql物理备份用的比较多的是innobackupex命令,备份常用,但对于里面的两个参数slave-info.safe-slave-backup一直搞的不太明白,今儿亲测了一下. 先解释一下参数意 ...
- android中的菜单Menu
Menu是一个接口, 他有2个子接口, SubMenu和ContextMenu; 前者菜单常见于按home键就会出现 ,后者是一个上下文菜单, 一般点击某个条目会在手机屏幕中出现一个类似悬浮窗口的菜单 ...
