在Angular2中,在某些情况下,我需要复制节点而不是移动它。该节点具有angular2属性,因此cloneNode不起作用。我该怎么做?
*什么不起作用
let el = <HTMLElement>document.getElementById(divId);
if ((<HTMLElement>el.parentNode).id == 'itsMe')
el = <HTMLElement>el.cloneNode(true);
document.getElementById(anotherId).appendChild(el);
Run Code Online (Sandbox Code Playgroud)
*什么工作有效,来自Angular2:克隆组件/ HTML元素及其功能
@Component({
selector: 'my-app',
template: `
<template #temp>
<h1 [ngStyle]="{background: 'green'}">Test</h1>
<p *ngIf="bla">Im not visible</p>
</template>
<template [ngTemplateOutlet]="temp"></template>
<template [ngTemplateOutlet]="temp"></template>
`
})
export class AppComponent {
bla: boolean = false;
@ContentChild('temp') testEl: any;
}
Run Code Online (Sandbox Code Playgroud)
但是如何动态添加模板?