源代码:

<h1>ng-template</h1>
<br><!-- 通过ngIf结构型指令显示ng-template的内容 -->
<div class="lessons-list" *ngIf="!condition else elseTemplate"><h3>标题:判断条件为真</h3>
</div>
<ng-template #elseTemplate><div>判断条件为假</div>
</ng-template>
<br>

运行时,准备渲染 id 为 elseTemplate 的模板:

TemplateRef有个方法:createEmbeddedView

兄弟comment节点:

TemplateRef对应ng-template的引用。

看一个实际的例子:

template 文件里,定义一个id为tpl的ng-template:

<ng-template #tpl><div>Hello, ng-template!</div>
</ng-template>

在宿主文件里:

import {AfterViewInit, Component, TemplateRef, ViewChild, ViewContainerRef} from '@angular/core';@Component({selector: 'app-ng-template',templateUrl: './ng-template.component.html'
})
export class NgTemplateComponent implements AfterViewInit {condition = true;@ViewChild('tpl')tplRef: TemplateRef<any>;myContext = {$implicit: '默认值', name: 'tuacy'};constructor(private vcRef: ViewContainerRef) {}ngAfterViewInit(): void {this.vcRef.createEmbeddedView(this.tplRef);}}

  1. 首先使用依赖注入,拿到ViewContainerRef引用。
  2. 使用@ViewChild, 拿到id为tpl的ng-template引用,类型为TemplateRef.
  3. 调用ViewContainerReference, 创建tpl模板实例。

注意,this.vcRef.createEmbeddedView, 虽然传入的是templateRef,但实际上正是调用templateRef的createEmbeddedView方法。

创建完view后立即渲染:

2021-4-20

Represents an embedded template that can be used to instantiate embedded views.

TemplateRef代表一个嵌入的模板,可以被用来实例化嵌入视图。

abstract class TemplateRef<C> {abstract elementRef: ElementRefabstract createEmbeddedView(context: C): EmbeddedViewRef<C>
}

elementRef:The anchor element in the parent view for this embedded view.

更多Jerry的原创文章,尽在:“汪子熙”:

ng-template 和 TemplateRef.createEmbeddedView相关推荐

  1. templateRef.createEmbeddedView is not a function

    背景:在使用Swper封装一个组件时,碰到这样一种常见,使用*ngIf判断数据类型,如果为数组就去循环dom,如果不是就直接使用ng-template的ngTemplateOutlet去接收,但是碰到 ...

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

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

  3. flask框架视图和路由_角度视图,路由和NgModule的解释

    flask框架视图和路由 Angular vs AngularJS (Angular vs AngularJS) AngularJS (versions 1.x) is a JavaScript-ba ...

  4. 关于 ng-template 通过 @input 传入另一个 Component 不能工作的问题调试

    问题描述 本文设计到的代码仓库:https://github.com/wangzixi-diablo/ngDynamic host Component: <ng-template #paramT ...

  5. 关于施加在 div 标签上的 ngTemplateOutlet 指令让 div class 丢失的问题调试

    本文描述的应用代码地址:https://github.com/wangzixi-diablo/ngDynamic 问题描述 我在 div 标签页里使用 ngTemplateOutlet 给代码第 11 ...

  6. templateref html内容,angular之ng-template模板加载

    angular之ng-template模板加载 2019-01-07 编程之家 https://www.jb51.cc 编程之家收集整理的这篇文章主要介绍了angular之ng-template模板加 ...

  7. NG Ng-template(模板元素)

    ng-template <ng-template>是一个 Angular 元素,它永远不会直接显示出来. 在渲染视图之前,Angular 会把<ng-template>及其内容 ...

  8. templateref html内容,angular4中的ElemenetRef和TemplateRef之间的区别

    我见过很多ElemenetRef和TemplateRef的例子让我更加困惑. > ElementRef和TemplateRef之间的区别是什么我们应该使用另一个 this is my eleme ...

  9. 手写ngIf,使用视图容器引用ViewContainerRef和模板引用TemplateRef

    写这篇博客的原有:虽然Angular官网有提到指令的使用,以及相关Ref的使用,但是对相关Ref并没有做出详细的解释,也没有提供相关的详细文档. 明确 *ngIf 是一个结构型指令,需要 @Direc ...

最新文章

  1. Grunt-几个常用的任务配置,加载,执行的写法
  2. centos 6是否已安装mysql_Centos6安装mysql
  3. PowerDesigner中在生成的数据库脚本中用name列替换comment列作为字段描述的方法
  4. Visual Studio 2017新版发布,极大提高开发效率丨附下载 1
  5. java基本字符串_java – c基本字符串操作
  6. js 利用数组队列模拟多线程操作
  7. Mac上最好的白噪音软件:Noizio for Mac
  8. 听说C站还有人缺图片素材?一篇跟UI设计师要来的——图片网址整理大全 让你从此远离素材荒!(强烈建议收藏)
  9. Python数学建模系列(九):回归
  10. 十种常用的的数据分析思路,你都知道吗?
  11. 画费氏数列螺线的代码
  12. 【7】OPencv骨架细化算法
  13. 蚂蚁金服Java岗内推,quartz定时器的处理
  14. 局域网IP地址查看软件V2.0使用教程 Python办公自动化
  15. js 操作数组 push splice
  16. 万能密码为什么能成功
  17. HCIE-storage-lab 考试注意事项,实验考试细节及问题等
  18. C#毕业设计——基于C#+asp.net+SQL Server的公交车管理系统设计与实现(毕业论文+程序源码)——公交车管理系统
  19. R中的留一法交叉验证(带有示例)
  20. 6.2、串连ViewResolver(Chaining ViewResolvers)

热门文章

  1. 基于php5.6 php.ini详解
  2. sublime text全程指南【转载】
  3. JavaScript中为何要使用prototype
  4. Zygote工作流程分析
  5. 使用Java8新特性(stream流、Lambda表达式)实现多个List 的笛卡尔乘积 返回需要的List<JavaBean>
  6. Linux下安装VMware Tools 的方法
  7. mac与windows服务器 访问和共享
  8. Linux下KVM虚拟机基本管理及常用命令(转)
  9. java.util -- List接口
  10. ASP.NET安全[开发ASP.NET MVC应用程序时值得注意的安全问题](转)