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

[Angular 2] Create template with Params

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

标签:

Angular 2 templates have a special let syntax that allows you to define and pass a context when they’re being generated.

 

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,
            {
                desc: Good
            }
        )
    }

}

 

We pass in an Object "desc: Good",  to the template, in tempalte, we use "let-" keyword to define the variable which we can reference to, and the object will be the ‘context‘ for the template.

<template #template let-desc="desc">
    <h3>tempalte {{desc}}</h3>
    <section>tempalte {{desc}}</section>
    <footer>template {{desc}}</footer>
</template>

 

[Angular 2] Create template with Params

标签:

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

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