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. String类的intern()方法,随常量池发生的变化

    JVM的知识这里总结的很详细:https://github.com/doocs/jvm/blob/master/README.md,因此在本博客也不会再对其中的东西重复总结了. intern的作用 简 ...

  2. SpringMVC中的generator

    引言 今天在做一个原生的spring项目的时候碰到一个非常好用的代码自动生成器,叫做generator,主要是运用于mybatis中的代码生成,它可以生成mapper的映射xml,model中的实体类 ...

  3. txt 文件的归并和分割

    1.归并 import os # 1.获取需要整合的文件目录下的 filepath = "G:\\我的python\\尹成\\python基础\\day13\\详细分类\\详细地区" ...

  4. “玲珑杯”ACM比赛 Round #18 1147 - 最后你还是AK了(思维,边的贡献)

    题目链接:http://www.ifrog.cc/acm/problem/1147 题解:这题很容易想到的是边的贡献也就是每条边最多被取到几次,和点的贡献类似,那些加边只要加在边贡献大的边上就行.然后 ...

  5. JS-特效 ~ 01. 事件对象、offset偏移/检测、无缝滚动、自动循环轮播图

    Math.round ( ) :正书四舍五入,负数五舍六入 用定时器,先清除定时器 事件对象 event event:事件被触动时,鼠标和键盘的状态,通过属性控制 Offset:偏移,检测 1. 获取 ...

  6. win10 设定计划任务时提示所指定的账户名称无效,如何解决?

    我想把我的 python 爬虫脚本设定为自动定时执行,我的设备是win10 操作系统,这将用到系统自带的计划任务功能.且我希望不管用户是否登录都要运行该定时任务,但在设置计划任务的属性时,遇到一个报错 ...

  7. 一次搞懂建模语言UML

    Unified Modeling Language (UML)又称统一建模语言或标准建模语言,它是一个支持模型化和软件系统开发的图形化语言,为软件开发的所有阶段提供模型化和可视化支持,包括由需求分析到 ...

  8. Android 网络通信框架Volley(三)

    NetworkImageView 分析:public class NetworkImageView extends ImageView 他继承自ImageView,并且添加了一个新方法: public ...

  9. myeclipse 保存含中文的jsp失败,提示内容含有 ISO-8859-1 不支持的字符

    就是这货,网上说各种设置首选项编码神马的,但我只是临时学一学jsp,装的myeclipse貌似不全,没有他们说的选项,后来发现了解决方案: 出错是因为我的jsp文件是用于在其他jsp中引入的,所以没有 ...

  10. Hadoop学习笔记之HBase Shell语法练习

    Hadoop学习笔记之HBase Shell语法练习 作者:hugengyong 下面我们看看HBase Shell的一些基本操作命令,我列出了几个常用的HBase Shell命令,如下: 名称 命令 ...