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. JavaScript--引用JS外部文件

    通过前面知识学习,我们知道使用<script>标签在HTML文件中添加JavaScript代码,如图: JavaScript代码只能写在HTML文件中吗?当然不是,我们可以把HTML文件和 ...

  2. ios TextField 不被键盘遮住

    首先放一个scrollView窗口,将Scroll View视图占整个屏幕. 向Scroll View    添加TextField 控件. 首先,ViewController.h  代码如下; #i ...

  3. [Android]异常2-Unexpected error while executing

    异常原因: 可能一>Android Studio的自动编译没有成功 解决方法有: 解决一>菜单栏里的“Build”,“Clean Project” 注:

  4. Java Web框架前景浅析

    基于三(多)层架构模式,典型WEB系统的总体架构如下图所示: 在上述分层架构中,整个应用被划分为两大部分: 客户端:基于浏览器提供信息展现.用户交互等功能.所采用的技术主要有:HTML/HTML5.J ...

  5. Canvas——基本入门

    基本概念 1.canvas 是 HTML5 提供的一个用于展示绘图效果的标签. canvas 原意画布, 帆布. 在 HTML 页面中用于展示绘图效果. 最早 canvas 是苹果提出的一个方案, 今 ...

  6. 学习java编程能往哪些方向发展

    当下Java训练非常热,是因为通过学习java能够快速的就业,这对于今年就业压力非常大的大学生来说,无疑是一条就业的捷路,虽然培教育费动辄过万,但还是非常值得的. 可是你可曾想过,学习了java编程后 ...

  7. 白盒-CNN纹理深度可视化: 使用MIT Place 场景预训练模型

    MIT发文:深度视觉的量化表示................ Places2 是一个场景图像数据集,包含 1千万张 图片,400多个不同类型的场景环境,可用于以场景和环境为应用内容的视觉认知任务. ...

  8. Find the build UUID in a Crash Report

    1) Find the build UUID in a Crash Report The first line in the "Binary Images:" section of ...

  9. plsql developer连接oracle数据库

    1.下载安装PLSQL Developer12 访问PLSQL Developer官网https://www.allroundautomations.com/bodyplsqldevreg.html, ...

  10. Java切换JDK版本的方法及技巧

    由于项目的不同安排,之前项目开发时,使用的jdk版本为1.8,现临时接手一以前项目,需要更换jdk版本. 安装 不再赘述,去Oracle网站(https://www.oracle.com/techne ...