ng-template

<ng-template>是一个Angular元素,用来渲染HTML,它不会直接展示在页面上,需要通过结构型指令将内容渲染到页面中。

在我们日常开发中见到的结构型指令,如 *ngIf,ngFor 是一个微语法,解开之后会变成 <ng-template>标签。在介绍<ng-template>之前我们下看一下ngIf解开语法糖,会是怎样的一个结构

*ngIf

<div *ngIf="hero" class="name">{{hero.name}}</div><!-- *ngIf翻译成ng-template元素之后 -->
<ng-template [ngIf]="hero"><div class="name">{{hero.name}}</div>
</ng-template>
  • *ngIf变成属性型指令,并绑定在ng-template元素上
  • *ngIf的宿主元素被包含在ng-template元素内

ng-template用法

用法1

利用指令插入模板

import { Directive, Input, TemplateRef, ViewContainerRef } from '@angular/core';@Directive({ selector: '[appUnless]' })
export class UnlessDirective {constructor(private templateRef: TemplateRef<any>,private viewContainer: ViewContainerRef) { }@Input() set appUnless(condition: boolean) {if (!condition) {// 容器内插入模板this.viewContainer.createEmbeddedView(this.templateRef);}else {// 清除容器内模板this.viewContainer.clear();}}
}

app.component.ts

import { Component, ViewChild, OnInit } from '@angular/core';@Component({selector: 'app-root',templateUrl: './app.component.html',styleUrls: []
})
export class AppComponent {condition = true;
}

app.component.html

<button (click)="condition = !condition">toggle</button><div *appUnless="condition"></div><ng-template [appUnless]="condition"><p class="code unless">(A) &lt;ng-template [appUnless]="condition"&gt;</p>
</ng-template>

用法2

使用ViewContainerRef操作DOM

import { Component, ViewContainerRef, OnInit, ViewChild, ElementRef } from '@angular/core';@Component({selector: 'app-root',templateUrl: './app.component.html',styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
// $implicit 使用默认值context = { $implicit: 'World', name: 'liyq' }@ViewChild('myTpl') tpl: ElementRef;constructor(private vr: ViewContainerRef) { }ngOnInit() {// 实例化一个内嵌视图,并把它插入到该容器中。this.vr.createEmbeddedView(this.tpl, { name: 'nihao' });this.vr.get(0);// this.vr.clear();}
}

app.component.html


<ng-container *ngTemplateOutlet="myTpl; context: context"></ng-container><ng-template #myTpl let-name="name"><h2>this is myTpl {{ name }}</h2>
</ng-template>

结果展示

ng-template相关推荐

  1. cannot bind to cxOutlet since it is not a known property of ng template

    我在SAP Spartacus处于运行状态(即ng serve)始终处于运行状态时,进行代码热修改,经常遇到此类错误消息 退出ng serve,重新执行即可: 更多Jerry的原创文章,尽在:&quo ...

  2. 必须正确理解的---ng指令中的compile与link函数解析

    这个绝对是深入的知识,但看完之后,对NG的理解就很利害啦. http://www.ifeenan.com/angularjs/2014-09-04-%5B%E8%AF%91%5DNG%E6%8C%87 ...

  3. NG客制项目下的I18n国际化标准方案

    方案选择 国际化i18n ​ 这个方案是最成熟的,同时也是官方的方案,但是这样一个标准化的方案同时意味着灵活度不够.当需要划分feature module,需要客制化组件的时候,这个方案的实施的成本就 ...

  4. [译]ng指令中的compile与link函数解析 转

    通常大家在使用ng中的指令的时候,用的链接函数最多的是link属性,下面这篇文章将告诉大家complie,pre-link,post-link的用法与区别. 原文地址 angularjs里的指令非常神 ...

  5. ng bootstrap css,Angular2学习笔记-ng bootstrap中motal组件使用

    ng bootstrap是一个angular UI bootstrap库,里面包含了一些基本的UI组件(日期选择组件,按钮,下拉框等).目前的版本为v1.0.0-alpha.14,基于angular2 ...

  6. 微信小程序模板template

    上面是官方的讲解, 主要是方便在不同的地方调用. 下面自己说下使用, 先创建一个模板名字是自己随便取的, 在template.wxml中填写模板 最外层用template标签 设置一个name属性 & ...

  7. ng new ng-pro 报错(创建angular6项目报错)

    1.安装nodejs D:\develop\nodejs 2.配置node  npm环境变量,npm本地仓库指向自定义目录 原npm仓库位置C:\Users\Administrator\AppData ...

  8. C++中模板template typename T

    template <typename Dtype>. 网上解释的非常多,觉得比较啰嗦,其实就是一个类型模板. 比如我们要计算两个数的加法,针对不同类型可能需要设计不同类型的函数,那么tem ...

  9. 让你爽到飞起的【懒人插件AutoScssStruct4Vue】VSCode根据template的标签目录一键生成SCSS/LESS结构,敏捷开发必备插件!!!

    Ctrl+Shift+X 搜索AutoScssStruct4Vue   如上图直接右键-autoScssStruct(你都不需要聚焦到template节点) 直接就给你妥妥的把SCSS代码写好了,如果 ...

  10. ng命令汇总:Angular CLI 使用教程指南参考

    Angular CLI 使用教程指南参考 Angular CLI 现在虽然可以正常使用但仍然处于测试阶段. Angular CLI 依赖 Node 4 和 NPM 3 或更高版本. 安装 要安装Ang ...

最新文章

  1. 【学习笔记】树上启发式合并
  2. 基于单例模式的日志输出(C++)
  3. 从大门看守到贴身保镖服务的安全纵深防御
  4. android 读取sd卡中的图片
  5. kprobes/kretprobes 在 bcc 程序中的使用
  6. oracle反调试,突破前端反调试--阻止页面不断debugger
  7. PMP读书笔记(第7章)
  8. dnf超时空漩涡副本路线流程图_DNF超时空漩涡机制
  9. 网页上无缝滚动的实现
  10. python照片过人脸_python图片人脸检测
  11. 逻辑漏洞(基本概念、爆破)
  12. IOS逆向学习-越狱环境的搭建
  13. javascript实现前程无忧的选择城市
  14. java take,Java DelayQueue take()用法及代码示例
  15. 智力游戏教案c语言代码,幼儿园智力游戏教案有哪些
  16. python lncrna_[转载]lncrna分析流程
  17. 轻量应用服务器和ecs云服务器哪个好
  18. 解决SVN Can’t open file ‘/XXX/xxx/db/txn-current-lock’错误
  19. python可视化小程序-实际利率与黄金价格走势图
  20. iOS 13适配汇总

热门文章

  1. 饿了么收购百度外卖后 又获得支付宝首页入口(转)
  2. HTML学生个人网站作业设计:动漫网站设计——动漫海贼王(5页) HTML+CSS+JavaScript 简单DIV布局个人介绍网页模板代码 DW学生个人网站制作成品下载
  3. Flannel 安装部署
  4. python去除水印
  5. python3中os.system(cmd)的返回值
  6. PowerPC栈帧理解
  7. 念起即断,念起不随,念起即觉,觉之既无,
  8. 快速判断文件是否存在
  9. Ubuntu系统下的shell 命令
  10. PowerSploit PowerUp模块提权