【发布时间】:2017-06-14 13:44:25
【问题描述】:
在 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);
*什么可行,来自Angular2: Cloning component / HTML element and it's functionality
@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;
}
但是如何动态添加模板呢?
【问题讨论】:
-
你试过用
ngFor吗? -
有什么用?我想动态地做到这一点
标签: angular dom clonenode ng-template