相关疑难解决方法(0)

如何在angular2中动态添加克隆的节点(等同于cloneNode)

在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)

但是如何动态添加模板?

dom clonenode ng-template angular

2
推荐指数
1
解决办法
7060
查看次数

标签 统计

angular ×1

clonenode ×1

dom ×1

ng-template ×1