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. Eclipse Paho MQTT Utility

    下载地址: https://repo.eclipse.org/content/repositories/paho-releases/org/eclipse/paho/org.eclipse.paho. ...

  2. 手把手教你用深度学习做物体检测(六):YOLOv2介绍

    本文接着上一篇<手把手教你用深度学习做物体检测(五):YOLOv1介绍>文章,介绍YOLOv2在v1上的改进.有些性能度量指标术语看不懂没关系,后续会有通俗易懂的关于性能度量指标的介绍文章 ...

  3. 2019DX#8

    Solved Pro.ID Title Ratio(Accepted / Submitted)   1001 Acesrc and Cube Hypernet 7.32%(3/41)   1002 A ...

  4. P2746 [USACO5.3]校园网Network of Schools tarjan 缩点

    题意 给出一个有向图,A任务:求最少需要从几个点送入信息,使得信息可以通过有向图走遍每一个点B任务:求最少需要加入几条边,使得有向图是一个强联通分量 思路 任务A,比较好想,可以通过tarjan缩点, ...

  5. 【Windows】PostgreSql安装

    Installer安装包问题 Problem running post-install step. Installation may not complete correctly. The datab ...

  6. Spring中常用的23中设计模式

    1.spring 中常用的设计模式有23中  分类  设计模式  创建型 工厂方法模式(FactoryMethod).抽象工厂模式(AbstractFactory).建造者模式(Builder).原型 ...

  7. 从“HDU 2005 第几天?”谈起

    在程序设计中,日期时间的处理经常会遇到.在C语言程序设计的一些教材中会出现如下例子或习题. [例1]第几天? (HDU 2005) 给定一个日期,输出这个日期是该年的第几天. Input输入数据有多组 ...

  8. java第一次测验

    package kaoshi; import java.util.Scanner; public class ScoreManagement { static int t=0; static int ...

  9. java对象与json字符串的互相转换

    java对象与json字符串的互相转换 1.采用 net.sf.json.JSONObject maven依赖包: <dependency> <groupId>net.sf.j ...

  10. IDEA中把一个Maven工程安装到本地仓库

    首先说明问题,我需要使用PageHelper插件,但是这个分页插件是改造过的,是一个pom工程,现在需要当作一个jar包使用,所以需要安装到本地仓库 1. 2. 3. 4. 5.成功 6.需要使用该j ...