[Angular 2] Set Values on Generated Angular 2 Templates with Template Context

Angular2模板上下文传递
本文介绍Angular2中模板的特殊let语法,演示如何定义并传递上下文数据。通过一个实例展示了如何创建模板并在运行时为其提供数据。

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

 

We have template:

<template #template let-description="description">
    <h2>My {{description}} template</h2>
    <button>My {{description}} button</button>
</template>

And we define 'description' variable to let data to pass into.

 

import {Component, ViewChild, ViewContainerRef, ComponentFactoryResolver} from "@angular/core";
import {WidgetThree} from "../widgets/widget-three.component";
@Component({
    selector: 'home',
    template: `
<button (click)="onClick()">Create Template</button>
<div #container></div>

<template #template let-description="description">
    <h2>My {{description}} template</h2>
    <button>My {{description}} button</button>
</template>
`
})
export class HomeComponent{
    @ViewChild('container', {read:ViewContainerRef}) container;
    @ViewChild('template') template;

    constructor(){}

    onClick(){
        this.container.createEmbeddedView(this.template, {
            description: 'sweet'
        });
    }
}

 

转载于:https://www.cnblogs.com/Answer1215/p/6075093.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值