Angular2,如何将变量传递给克隆的模板

GWo*_*ing 0 cloning angular2-template angular

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

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

import {Component, NgModule, ViewChild, ViewContainerRef} from '@angular/core'
import {BrowserModule} from '@angular/platform-browser'

@Component({
  selector: 'my-app',
  template: `
    <template #clone let-session>
     <div [id]="session">eps {{session}} ya</div>
    </template>

    <div #container></div>

    <div>
      <button (click)="create()">Hello</button>
    </div>
  `,
})
export class App {
  name:string;

    @ViewChild('clone') clone:any;
    @ViewChild('container', {read:ViewContainerRef}) container:any;

  constructor() {
    this.name = 'Angular2'
  }

  create(){
    let session = 'testing';
        this.container.createEmbeddedView(this.clone, {context: {$implicit: session}});
  }
}

@NgModule({
  imports: [ BrowserModule ],
  declarations: [ App ],
  bootstrap: [ App ]
})
export class AppModule {}
Run Code Online (Sandbox Code Playgroud)

和 plunker https://plnkr.co/edit/pWeQfTLroOjKoyKnaZvL?p=preview

kem*_*sky 5

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

<template #clone let-context="context">
 <div [id]="context.session">eps {{context.session}} ya</div>
</template>

this.container.createEmbeddedView(this.clone, {context: {session: session}});
Run Code Online (Sandbox Code Playgroud)