[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 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.
[Angular] Create dynamic content with <tempalte>的更多相关文章
- Create Dynamic Modal Dialog Form in AdminLTE Bootstrap template
原文地址 Create modal dialog form in jquery using bootstrap framework, slightly different from the usual ...
- Django Views: Dynamic Content
世味年来薄似纱,谁令骑马客京华. 小楼一夜听春雨,深巷明朝卖杏花. 矮纸斜行闲作草,晴窗细乳戏分茶. 素衣莫起风尘叹,犹及清明可到家. Your Second View: Dynamic Conten ...
- android 使用Tabhost 发生could not create tab content because could not find view with id 错误
使用Tabhost的时候经常报:could not create tab content because could not find view with id 错误. 总结一下发生错误的原因,一般的 ...
- swipe js dynamic content
swipe js dynamic content swipe 动态改变内容时,需要用 update 一下. swiper.update(true); 实例: HTML Code 页面用的FreeMa ...
- [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 ...
- [Angular] Using ngTemplateOutlet to create dynamic template
I can use <tamplete> syntax and a entry component as a container to create a dynamic component ...
- Create Your Content and Structure
The original page source Content is the most important aspect of any site. So let's design for the c ...
- [Angular] Create a custom validator for reactive forms in Angular
Also check: directive for form validation User input validation is a core part of creating proper HT ...
- [Angular] Create a custom validator for template driven forms in Angular
User input validation is a core part of creating proper HTML forms. Form validators not only help yo ...
随机推荐
- 洛谷 P2616 [USACO10JAN]购买饲料II Buying Feed, II
洛谷 P2616 [USACO10JAN]购买饲料II Buying Feed, II https://www.luogu.org/problemnew/show/P2616 题目描述 Farmer ...
- Linux经常使用命令(七) - cp
cp命令用来拷贝文件或者文件夹.是Linux系统中最经常使用的命令之中的一个.普通情况下.shell会设置一个别名.在命令行下拷贝文件时,假设目标文件已经存在.就会询问是否覆盖.无论你是否使用-i參数 ...
- startActivity时报错Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag
startActivity时报错Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVI ...
- 基于cropper.js的图片上传和裁剪
项目中要求图片上传并裁剪的功能,之前也有接触过很多图片裁剪插件,效果体验不是很好,今天推荐一款好用的插件-cropper,超级好用,裁剪功能丰富,满足了各种需求. 功能: 1:点击选择图片,弹出文件夹 ...
- 2017.1-TOP5 Android开源库
Colorful (Github) Colorful简单实用,通过这个开源库可以通过编码的方式来改变应用的主题,不再需要定义不同的style dependencies { compile 'com.g ...
- amazeui学习笔记--css(HTML元素2)--代码Code
amazeui学习笔记--css(HTML元素2)--代码Code 一.总结 1.行内代码:code标签<code> 2.代码片段:pre标签<pre> 3.限制代码块高度:添 ...
- [转]C#连接操作mysql实例
本文转自:http://hi.baidu.com/zhqngweng/item/c4d2520cb7216877bfe97edf 第三方组件:Mysql.Data.dll说明:去官方网站下载Mysql ...
- angular 创建服务
一:新建服务模块和服务文件 ng g module services --spec=false ng g service services/quote --spec=false 二:在quote.se ...
- 【Codeforces Round #301 (Div. 2) B】 School Marks
[链接] 我是链接,点我呀:) [题意] 已知k门成绩. 总共有n门成绩. 让你构造剩下的n-k门成绩,使得这n门成绩的中位数>=y,并且这n门成绩的和要小于等于x. n为奇数 [题解] 首先判 ...
- [D3] Create DOM Elements with D3 v4
Change is good, but creating from scratch is even better. This lesson shows you how to create DOM el ...