Angular 2 Components have templates, but you can also create templates inside of your templates using Angular 2 ViewContainer’s createEmbeddedView for more visual control of how you want to generate elements inside of your Angular 2 templates.

import {Component, ViewChild, ViewContainerRef, ComponentFactoryResolver, ViewChild} from '@angular/core';
import {SimpleService} from "../../serivces/simple.service";
import {WidgetThree} from "../widgets/widget-three.component"; @Component({
moduleId: module.id,
selector: 'home',
templateUrl: 'home.component.html'
})
export class HomeComponent { @ViewChild('container', {
read: ViewContainerRef
}) container;
@ViewChild('template') template; constructor(private resolver: ComponentFactoryResolver, private simpleService: SimpleService) {
} ngAfterContentInit(){
const WidgetFactory = this.resolver.resolveComponentFactory(WidgetThree);
this.container.createComponent(WidgetFactory);
this.container.createComponent(WidgetFactory);
this.container.createComponent(WidgetFactory);
this.container.createComponent(WidgetFactory);
this.container.createComponent(WidgetFactory);
} onClick(){
this.container.createEmbeddedView(
this.template
)
} }
<template #template>
<h3>tempalte title</h3>
<section>tempalte section</section>
<footer>template footer</footer>
</template>

[Angular 2] Generate and Render Angular 2 Template Elements in a Component的更多相关文章

  1. [Angular 2] Generate Angular 2 Components Programmatically with entryComponents & ViewContainerRef

    You can generate Angular 2 components using a combination of ViewContainer and ComponentFactory, but ...

  2. [Angular] Use ngx-build-plus to compile Angular Elements

    We can treat Angular Element as each standlone lib and compile each Angular element spreatly. Tool w ...

  3. Angular入门到精通系列教程(7)- 组件(@Component)基本知识

    1. 概述 2. 创建Component 组件模板 视图封装模式 特殊的选择器 :host inline-styles 3. 总结 环境: Angular CLI: 11.0.6 Angular: 1 ...

  4. [Angular] Communicate Between Components Using Angular Dependency Injection

    Allow more than one child component of the same type. Allow child components to be placed within the ...

  5. [Angular] ngx-formly (AKA angular-formly for Angular latest version)

    In our dynamic forms lessons we obviously didn’t account for all the various edge cases you might co ...

  6. angular源码分析:angular中脏活累活的承担者之$interpolate

    一.首先抛出两个问题 问题一:在angular中我们绑定数据最基本的方式是用两个大括号将$scope的变量包裹起来,那么如果想将大括号换成其他什么符号,比如换成[{与}],可不可以呢,如果可以在哪里配 ...

  7. angular源码分析:angular中入境检察官$sce

    一.ng-bing-html指令问题 需求:我需要将一个变量$scope.x = '<a href="http://www.cnblogs.com/web2-developer/&qu ...

  8. angular的跨域(angular百度下拉提示模拟)和angular选项卡

    1.angular中$http的服务: $http.get(url,{params:{参数}}).success().error(); $http.post(url,{params:{参数}}).su ...

  9. angular源码分析:angular的整个加载流程

    在前面,我们讲了angular的目录结构.JQLite以及依赖注入的实现,在这一期中我们将重点分析angular的整个框架的加载流程. 一.从源代码的编译顺序开始 下面是我们在目录结构哪一期理出的an ...

随机推荐

  1. 【126】win8的一些问题

    1.win8 窗口背景色修改 在Windows默认主题下,打开注册表编辑器(win键+R,即运行,输入regedit),依次双击打开HKEY_CURRENT_USER\Control Panel\Co ...

  2. jQuery Callback 函数

    @(编程) Callback 函数在当前动画 100% 完成之后执行. jQuery 动画的问题 许多 jQuery 函数涉及动画.这些函数也许会将 speed 或 duration 作为可选参数. ...

  3. hdu 1045 Fire Net(最小覆盖点+构图(缩点))

    http://acm.hdu.edu.cn/showproblem.php?pid=1045 Fire Net Time Limit:1000MS     Memory Limit:32768KB   ...

  4. Android 显示/隐藏 应用图标

    PackageManager packageManager = getPackageManager(); ComponentName componentName = new ComponentName ...

  5. SQL2005/8数据库提示单个用户无法操作的解决方法

    原因分析: 是操作数据库的用户被锁定了,思路是通过查找目标用户,将其解锁即可,可是这样太麻烦了. 解决办法执行如下sql: USE master;  GO  DECLARE @SQL VARCHAR( ...

  6. 使用XML与远程服务器进行交互

    最近在做的一个项目其中的一部分是与远程服务器进行交互,确定身份验证的合法性,于是编写了SendRequest方法 此方法发送给远程服务器XML请求,服务器经过处理后,返回XML回应,由此方法接收到后进 ...

  7. Spring MVC体系结构

    [Spring MVC类图]<Spring实战>中:<Spring3.0就这么简单>中:[http://blog.csdn.net/gstormspire/article/de ...

  8. HTML5 ajax上传附件

    var fd = new FormData();             fd.append('/*键值*/', this.files[0]);var xhr = new XMLHttpRequest ...

  9. oracle:自定义多行合并聚合函数

    原始表 COUNTRY CITY            -------------------- -------------- 中国 台北              中国 香港             ...

  10. jQuery Attributes vs. Properties

    Attributes vs. Properties attributes和properties之间的差异在特定情况下是很重要.jQuery 1.6之前 ,.attr()方法在取某些 attribute ...