2017-01-29 10 views
1

Angular2에서 일부 노드를 이동하는 대신 복제해야합니다. 그 노드는 angular2 속성을 가지고 있으므로 cloneNode는 작동하지 않습니다. 내가 어떻게 해?angular2에 복제 노드를 동적으로 추가하는 방법 (cloneNode와 동일)

* 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; 
} 

하지만 어떻게에서

let el = <HTMLElement>document.getElementById(divId); 
    if ((<HTMLElement>el.parentNode).id == 'itsMe') 
     el = <HTMLElement>el.cloneNode(true); 
    document.getElementById(anotherId).appendChild(el); 

* 어떻게 일하는 것이이 동적으로 템플릿을 추가하기 위해 무엇을 작동하지 않는 이유는 무엇입니까? 당신이 무슨 짓을했는지 기본적있는 <template> 태그이

내부 복제 수동 요소를 포장 만 대신 밖으로 인쇄 -

<p>Paragraph One</p> 
<p>Paragraph Two</p> <!-- Let's try to clone this guy --> 
<p>Paragraph Three</p> 

옵션 1 :

+0

'ngFor'를 사용해 보셨습니까? – yurzui

+0

무엇을 원하십니까? 나는 동적으로하고 싶다. – Gerard

답변