2017-11-27 7 views
0

에 온 클릭 이벤트 APPEND 구성 요소 나 각도 5를 사용하고 있는데이 있습니다각도 5 - 사업부

// app.component.ts 
import { Component } from '@angular/core'; 

@Component({ 
    selector: 'app-root', 
    templateUrl: './app.component.html', 
    styleUrls: ['./app.component.css'] 
}) 
export class AppComponent { 

    appendToContainer() { 
    // Do the stuff here 
    // Append PanelComponent into div#container 
    } 

} 

이제 app.component.html

// app.component.html 
<button (click)="appendToContainer()"></button> 
<div id="container"></div> 

그리고 마지막으로 내가 간단한 구성 요소가를

// panel.component.html 
<div class="panelComponent"> 
    This is a panel Component html 
</div> 

내가하고 싶은 그 때마다 내가 panel.component의 APPE 필요 클릭 app.component.html에있는 버튼입니다 app.component.html에 첨부 됨

어떻게해야합니까?

+0

을 원하는 달성하기 위해 * ngFor과 변수를 사용할 수 있습니다. '* ngFor'를 사용하여 배열을 반복해서는 안됩니까? –

+0

당신은'appendToContainer'를 호출 할 때 추가 할 수있는 객체 배열을 사용할 수 있으며 배열의 각 항목에 대한 패널을 만들기 위해'* ngFor'를 사용합니다. – FabioG

+0

무엇을하고 싶습니까? 컴포넌트에 새로운 인스턴스를 만들고 추가합니다. 단추를 클릭 할 때 div 안에 – pedromartinez

답변

0

당신은 당신이 당신이 잘못된 측면에서 그 문제를 가지고 느낌이

//In your component 
num:number=0 

//You html like 
<button (click)="num++"></button> 
//we create a array "on fly" and slice to get only a "num" items 
<div *ngFor="let item in [0,1,2,3,4,5,6,7,8,9].slice(0,num)" class="panelComponent"> 
    This is a panel Component html 
</div> 
+0

구성 요소에 새 인스턴스를 만들고 단추를 클릭하면 div 안에 추가합니다. – pedromartinez

+1

Perhapf 일부는 https://blog.dmbcllc.com/dynamically를 좋아합니다. - 부품 - 각도 - /? – Eliseo

+0

각도 5에 해당하는 코드가없는 것 같습니다. 내 app.component.ts가 있습니다 : '@ angular/core'의 import {Component}; '{/child/child.component'에서 {ChildComponent} 가져 오기; @Component ({ 선택 : '응용 프로그램 루트', templateUrl './app.component.html', styleUrls : [ './app.component.scss'] }) 수출 클래스 AppComponent { 생성자() {} addCmp() { console.log ('adding'); // ChildComponent 인스턴스 추가 } } – pedromartinez