I can use <tamplete> syntax and a entry component as a container to create a dynamic component. Notice it will create a empty div as a placeholder in the DOM. If we don't wanner this empty div, we can actually using ng-conainer together with ngTemplateOutlet (for template ref) and ngTemplateOutletContext (the context).

import { Component, TemplateRef, ComponentRef, ViewContainerRef, ViewChild, AfterContentInit, ComponentFactoryResolver } from '@angular/core';

import { AuthFormComponent } from './auth-form/auth-form.component';

import { User } from './auth-form/auth-form.interface';

@Component({
selector: 'app-root',
template: `
<div>
<div #entry></div>
<template #tmpl let-obj let-location="location">
<details>
<summary>{{obj.name}}</summary>
<p> - Age: {{obj.age}}</p>
<p> - Address :{{location}}</p>
</details>
</template>
<hr />
<ng-container
[ngTemplateOutlet]="tmpl"
[ngTemplateOutletContext]="ctx"
></ng-container>
</div>
`
})
export class AppComponent implements AfterContentInit { @ViewChild('entry', { read: ViewContainerRef }) entry: ViewContainerRef;
@ViewChild('tmpl') tmpl: TemplateRef<any>; ctx = {
$implicit: {
name: 'John',
age:
},
location: 'USA'
} ngAfterContentInit() {
this.entry.createEmbeddedView(this.tmpl, {
$implicit: {
name: 'Zhentian',
age:
},
location: 'China'
})
} }

And in the generated DOM we can see that there is no empty div created.

[Angular] Using ngTemplateOutlet to create dynamic template的更多相关文章

  1. [Angular] Create dynamic content with <tempalte>

    To create a dynamic template, we need to a entry component as a placeholder. Then we can use entry c ...

  2. Create Dynamic Modal Dialog Form in AdminLTE Bootstrap template

    原文地址 Create modal dialog form in jquery using bootstrap framework, slightly different from the usual ...

  3. 索引模板和动态索引模板 (Index Template和Dynamic Template)

    相关阅读 Index Templates https://www.elastic.co/guide/en/elasticsearch/reference/7.1/indices-templates.h ...

  4. [MST] Create Dynamic Types and use Type Composition to Extract Common Functionality

    Since MST offers a runtime type system, it can create and compose types on the fly, making it possib ...

  5. [Angular Directive] Create a Template Storage Service in Angular 2

    You need to define a <template> to be able to use it elsewhere in your app as a TemplateRef. Y ...

  6. [Angular] Use Angular style sanitization to mark dynamic styles as trusted values

    Angular has a very robust security model. Dynamically inserted html, style or url values into the DO ...

  7. [Angular] Using directive to create a simple Credit card validator

    We will use 'HostListener' and 'HostBinding' to accomplish the task. The HTML: <label> Credit ...

  8. [Angular 2] Using ng-for to repeat template elements

    This lesson covers Angular 2’s version of looping through data in your templates: ng-for. It’s conce ...

  9. [Angular] Two ways to create Angular Animation, using animation() or using state()

    We have two blocks to show to difference ways to do animation in Angular: <button (click)="t ...

随机推荐

  1. DataFrame入门案例(集团公司对人事信息处理场景)

    我用一个集团公司对人事信息处理场景的简单案例,来作为入门,详细分析DataFrame上的各种常用操作,包括集团子公司的职工人事信息的合并,职工的部门相关信息查询.职工信息的统计.关联职工与部门信息的统 ...

  2. Struts2之一 初体验

    Struts2 框架是基于MV模式开发的,它提供了一个核心控制器,用于对所有的请求进行统一处理,这个控制器是由一个名为FilterDispatcher的Servlet过滤器来充当的. 01.需要在we ...

  3. WinForm窗体项目 之 MySchool管理系统终极版

    学习WinForm窗体程序也有一段时间了,今天就来尝试着来一个项目热热身~ 在我们通常使用的MySchool管理中,不外乎这几种功能:增.删.改.查.改密码 在过去的C#中确实是挺简单的,但是在学习了 ...

  4. Objective-C——Runtime理解

    动态语言 OC是一门不折不扣的动态语言,所以它的很多机制都是动态运行时决定的.这点和C语言不一样,C语言是静态绑定,也就是编译后所有的一切都已经决定了.这一点和C语言的函数指针有些类似,很多时候函数指 ...

  5. MySQL的or/in/union与索引优化 | 架构师之路

    假设订单业务表结构为: order(oid, date, uid, status, money, time, …) 其中: oid,订单ID,主键 date,下单日期,有普通索引,管理后台经常按照da ...

  6. CSS——img标签消除3px

    1.dispaly:block 2.float:left 这两种都可以消除3px

  7. html5——动画案例(太阳系)

    太阳系主要利用定位,伪元素 <!DOCTYPE html> <html lang="en"> <head> <meta charset=& ...

  8. java中 数组 list map之间的互转

    三者之间转换关系,一张图清晰呈现. 上代码: 其中的maputils是apache的collection包. package util; import java.util.ArrayList; imp ...

  9. css特殊效果

    border-radius实现特殊形状 .box{ width: 100px; height: 100px; background: orange; border: 1px solid #000; b ...

  10. js弹开页面并调用方法

    每次重新写一个功能的时候,都能发现以前写的并不太好,都可以改进,奇怪的是我还是我,为什么曾经的我就想不起来要这么写,比如下面两段代码 历史代码: if (infoTablePage != null) ...