Angular 5 - onclick事件将组件附加到div中 [英] Angular 5 - onclick event append component into div

查看:94
本文介绍了Angular 5 - onclick事件将组件附加到div中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Angular 5,我有这个:

I am using Angular 5 and I have this:

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

Now the app.component.html

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

最后我有一个简单的组件

and finally I have a simple component

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

我想做的是每次app.component.html上的按钮点击时我需要一个panel.component追加到app.component.html

What I want to do is that everytime the button on app.component.html is click I need a panel.component appended into app.component.html

我该怎么做?

推荐答案

你可以使用* ngFor和变量来实现你想要的

You can use a *ngFor and a variable to achieve you want

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

这篇关于Angular 5 - onclick事件将组件附加到div中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆