2017-11-27 3 views
0

にコンポーネントを追加します。今すぐapp.component.html角度5 - のonclickイベント私は角度5を使用していますが、私はこれ持っているdiv要素

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

と最終的に私は単純なコンポーネントを持っているの

// 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 
    } 

} 

// 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のようなもの-Apart-in-Angle /? – Eliseo

+0

角5のコードがないようです。ここに私のapp.component.tsがあります:import @ Component/'@ angular/core'; '{/ child /child.component'から{ChildComponent}をインポートします。 @Component({ セレクタ: 'APP-ルート' templateUrl './app.component.html' styleUrls:[」./app.component.scss'] }) エクスポートクラスAppComponent { コンストラクタ(){} addCmp(){ console.log( 'adding'); // ChildComponentのインスタンスを追加する } } – pedromartinez

関連する問題