【发布时间】:2017-06-22 19:27:06
【问题描述】:
在 Angular2 中,我有一个模板代码,每当用户单击按钮时我都会克隆它,就像在此处回答的那样 How to dynamically add a cloned node in angular2 (equivalent to 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 {}
【问题讨论】:
-
为什么不直接将
id传递给指令的输入? -
我会读到它,现在我不知道该怎么做
-
查看更多代码会很有帮助。指令是什么?实际上,您甚至可以传递元素本身,因此您不必查询它。
-
我已经为指令添加了代码(如果有帮助的话)。
-
你在哪里进行克隆?为什么不保留参考?你想动态改变什么值?
标签: angular angular2-template cloning