Angular2,如何将变量传递给克隆的模板 [英] Angular2, how to pass a variable to a template that is cloned

查看:32
本文介绍了Angular2,如何将变量传递给克隆的模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Angular2 中,我有一个模板代码,每当用户单击按钮时我都会克隆它,就像这里回答的一样如何动态添加angular2中的一个克隆节点(相当于cloneNode)

我试图向它传递一个变量,但它不起作用.怎么了?

import {Component, NgModule, ViewChild, ViewContainerRef} from '@angular/core'从@angular/platform-b​​rowser"导入 {BrowserModule}@成分({选择器:'我的应用',模板:`<模板#clone let-session><div [id]="session">eps {{session}} ya</div><div #container></div><div><button (click)="create()">你好</button>

`,})出口类应用{名称:字符串;@ViewChild('clone') 克隆:任意;@ViewChild('container', {read:ViewContainerRef}) container:any;构造函数(){this.name = 'Angular2'}创建(){让会话 = '测试';this.container.createEmbeddedView(this.clone, {context: {$implicit: session}});}}@NgModule({进口:[浏览器模块],声明:[应用程序],引导程序:[应用程序]})导出类 AppModule {}

还有那个笨蛋https://plnkr.co/edit/pWeQfTLroOjKoyKnaZvL?p=preview

解决方案

我已经更新了您的 plunkr,基本上可以在 #8368 中找到答案: