To create a dynamic template, we need to a entry component as a placeholder. Then we can use entry component to create a new Tamplete into it.

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>
</div>
`
})
export class AppComponent implements AfterContentInit { @ViewChild('entry', { read: ViewContainerRef }) entry: ViewContainerRef;
@ViewChild('tmpl') tmpl: TemplateRef<any> ngAfterContentInit() {
this.entry.createEmbeddedView(this.tmpl, {
$implicit: {
name: 'Zhentian',
age: 27
},
location: 'China'
})
} }

As we can see, we defined:

let-obj let-location="location"

let-obj: because nothing is assigned to 'let-obj', it means it will show all the $implicit value we defined in when we 'createEmbeddedView' as a second arguement.

If we open dev tools, we can see there is a '<div></div>' placeholder was created and the template we created is actually not inject into the placeholder div block, it is below placeholder.

最新文章

  1. Java进击C#——语法之线程同步
  2. java JSP(原创新手可进)
  3. 2 django系列之django分页与templatetags
  4. eclipse+android+opencv环境搭建的步骤
  5. Java-继承 共3题
  6. Scalaz(41)- Free :IO Monad-Free特定版本的FP语法
  7. django 视图开发与url配置
  8. Hive_DDL与DML
  9. python model对象转为dict数据
  10. 4.0以后的新布局方式GridLayout
  11. Lipo Error!! can't open input file
  12. php中getimagesize函数的用法
  13. C# 线程间互相通信
  14. Protel 99SE铺铜问题总结
  15. 生成md5密码
  16. HDU/HDOJ 2612 Find a way 双向BFS
  17. SQL Server on Red Hat Enterprise Linux——RHEL上的SQL Server(全截图)
  18. linux命令行解刨
  19. eclipse中搜狗输入法中文状态下输出的全是英文
  20. JBOD

热门文章

  1. SSO单点登录学习总结(1)——单点登录(SSO)原理解析
  2. Servlet 规范笔记—基于http协议的servlet
  3. php操作zip压缩文件
  4. Redis的高级应用-安全性和主从复制
  5. 图解String类型的不可变性及其原因
  6. Anaconda的安装
  7. datagridview合并相同单元格
  8. 【例题 6-15 UVA - 10129】Play on Words
  9. POJ 题目2823 Sliding Window(RMQ,固定区间长度)
  10. js进阶 12-17 jquery实现鼠标左键按下拖拽功能