简体   繁体   English

在 Angular 11 中动态生成/生成现有组件

[英]spawn/generate existing components dynamically in Angular 11

I'm doing an web application that is supposed to work like the contacts app that we have in our phones:我正在做一个 web 应用程序,它应该像我们手机中的联系人应用程序一样工作:

  1. Register contact info (name, email, number)注册联系方式(姓名、email、号码)
  2. Click on save Button点击保存按钮
  3. List is updated immediately (here is my problem)列表立即更新(这是我的问题)

So I have a list.component (a grey mat-card) and a contact.component , (an orange mat-card).所以我有一个list.component (一张灰色垫卡)和一个contact.component (一张橙色垫卡)。

I want to spawn a new contact.component inside of the list.component , and destroy it when the user select the delete button.我想在list.component中生成一个新的contact.component ,并在用户 select 删除按钮时将其销毁。现在看起来像这样

The data is passing fine, but I don't know how to add a new existing component inside another.数据传递良好,但我不知道如何在另一个组件中添加新的现有组件。

You could save the contacts inside a public array and then iterate over it inside the list-component:您可以将联系人保存在公共数组中,然后在列表组件中对其进行迭代:

  <list-component>
<ng-container *ngFor="let contact of contactArray"> 
<contact-component></contact-component>
 </ng-container>
</list-component>

When the user adds another contact, you can push it to the contactArray and the additional contact-component will appear当用户添加另一个联系人时,您可以将其推送到contactArray,并且会出现额外的contact-component

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM