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的更多相关文章

  1. 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 ...

  2. Django.template框架 template context (非常详细)

    前面的章节我们看到如何在视图中返回HTML,但是HTML是硬编码在Python代码中的 这会导致几个问题: 1,显然,任何页面的改动会牵扯到Python代码的改动 网站的设计改动会比Python代码改 ...

  3. 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. ...

  4. Error resolving template: template might not exist or might not be accessible是一句缩水报错?

    一 thymeleaf在开发的时候本地调试正常,但是在测试环境打成jar包就报这个错误了. 二 template might not exist or might not be accessible ...

  5. 【报错】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 ...

  6. 【报错】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 ...

  7. 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 ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. 消费者、生产者Java代码示例,wait

    箱子中的苹果代表资源,现在有消费者从箱子中拿走苹果,生产者往箱子中放苹果.代码如下: 资源--箱子中的苹果: public class Box { int size; int num; public ...

  2. cogs2223. [SDOI2016 Round1] 生成魔咒(后缀数组 hash 二分 set

    题意:对一个空串每次在后面加一个字符,问每加完一次得到的字符串有几个不同的子串. 思路:每个子串都是某个后缀的前缀,对于每个后缀求出他能贡献出之前没有出现过的前缀的个数,答案累加就行. 要求每个后缀的 ...

  3. lightoj 1126 - Building Twin Towers(dp,递推)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1126 题解:一道基础的dp就是简单的递推可以设dp[height_left][ ...

  4. 数据库常用SQL语句(一):常用的数据库、表操作及单表查询语句

    以MySql数据库为例进行说明 1.数据库操作语句 2.表的操作语句 3.表中的字段操作语句 4.MYSQL支持的完整性约束 数据库管理系统提供了一致机制来检查数据库表中的数据是否满足规定的条件,以保 ...

  5. 史上最全面的SignalR系列教程-目录汇总

    1.引言 最遗憾的不是把理想丢在路上,而是理想从未上路. 每一个将想法变成现实的人,都值得称赞和学习. 致正在奔跑的您! 2.SignalR介绍 SignalR实现服务器与客户端的实时通信 ,她是一个 ...

  6. 基于DevExpress的SpreadsheetControl实现对Excel的打开、预览、保存、另存为、打印(附源码下载)

    场景 Winform控件-DevExpress18下载安装注册以及在VS中使用: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/1 ...

  7. 新建Servlet工程——IDEA

    Servlet是sun公司提供的一套接口规范,是运行在服务端的java程序.实现了Servlet的类能够被服务器识别,而普通的java类不能被识别. 1.新建工程 2. 3.工程名字“: 4.在WEB ...

  8. H2 数据库使用简介

    博客地址:http://www.moonxy.com 一.前言 H2 是一个用 Java 开发的嵌入式数据库,它本身只是一个类库,即只有一个 jar 文件,可以直接嵌入到应用项目中.H2 主要有如下三 ...

  9. 语音信号的梅尔频率倒谱系数(MFCC)的原理讲解及python实现

    梅尔倒谱系数(MFCC) 梅尔倒谱系数(Mel-scale FrequencyCepstral Coefficients,简称MFCC).依据人的听觉实验结果来分析语音的频谱, MFCC分析依据的听觉 ...

  10. .NET Conf 2019日程(北京时间)

    一年一度的 .NET Conf马上就要开始了,我将日程简易的翻译了一下,并且时间全部转换为北京时间,以方便国内.NETer. 日程 第1天 (北京时间9月24日) .NET Conf 2019 基调 ...