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. ResourceManager基本职能和内部架构

    YARN也采用了Master/Slave结构,其中,Master实现为ResourceManager,负责整个集群资源的管理与调度:Slave实现为NodeManager,负责单个节点的资源管理与任务 ...

  2. Java 线程池 ThreadPoolExecutor 的那些事儿

    线程池基础知识 ThreadPoolExecutor : 一个线程池 Executors : 线程池工厂,通过该类可以取得一个拥有特定功能的线程池 ThreadPoolExecutor类实现了Exec ...

  3. [python]通过微信公众号“Python程序员”,编写python代码

    今天发现微信公众号中,居然可以编写python代码,很是惊喜,觉得蛮有趣的. 步骤如下: 1.关注微信公众号“Python程序员” 2.关注成功后,点击右下角的“潘多拉”->"Pyth ...

  4. POJ 3067 Japan (树状数组求逆序对)

    POJ - 3067 题意:有(1-n)个城市自上到下在左边, 另有(1-m)个城市自上到下在右边,共有m条高速公路,现求这m条直线的交点个数,交点不包括在城市处相交. 题解:先将高速公路读入,然后按 ...

  5. codeforces 233 C. Cycles(贪心+思维)

    题目链接:http://codeforces.com/contest/233/problem/C 题意:在一个无相图中有N个长度为3 的回路,输出符合条件的图.注意此图的节点数不得超过100 题解:贪 ...

  6. Windows CVE-2019-0708 远程桌面代码执行漏洞复现

    Windows CVE-2019-0708 远程桌面代码执行漏洞复现 一.漏洞说明 2019年5月15日微软发布安全补丁修复了CVE编号为CVE-2019-0708的Windows远程桌面服务(RDP ...

  7. 【Offer】[4] 【二维数组中的查找】

    题目描述 思路分析 Java代码 代码链接 题目描述 在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数 ...

  8. 数据库常用SQL语句(二):多表连接查询

    前面主要介绍了单表操作时的相关查询语句,接下来介绍一下多表之间的关系,这里主要是多表数据记录的查询,也就是如何在一个查询语句中显示多张表的数据,这也叫多表数据记录的连接查询. 在实现连接查询时,首先是 ...

  9. BASK、BFSK、BPSK调制方法的Matlab程序实现

    %以下为手动编程方法,也可调用matlab集成函数dmod,具体调制方式见doc. n = [0:0.01:5.99]; x1 = ones(1,100); x2 = zeros(1,100); x3 ...

  10. Winform中使用FastReport实现自定义PDF打印预览

    场景 Winform中使用FastReport实现简单的自定义PDF导出: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/1009 ...