码迷,mamicode.com
首页 > 其他好文 > 详细

[Angular 2] Generate and Render Angular 2 Template Elements in a Component

时间:2016-09-23 21:24:24      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:

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

标签:

原文地址:http://www.cnblogs.com/Answer1215/p/5901581.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!