【问题标题】:Repeating a div element after a click [closed]单击后重复 div 元素[关闭]
【发布时间】:2021-02-18 09:25:49
【问题描述】:

我需要的是,单击按钮(添加)会生成一个由字段和删除按钮组成的表单。当您单击按钮时,它应该删除所有表单字段和删除按钮本身

<button type="button" class="btn btn-outline-success" (click)="AddBtn()"><span class="fa fa-plus"></span>Add</button>

<div *ngIf="isformes" >
  
<div>
  <form>
      <table>
          <div>
            
              <tr>
            
                  <td>
                      <mat-form-field ">
                          <mat-label>Name</mat-label>
                          <input matInput placeholder="Name"  autocomplete="off" required>
                      </mat-form-field>
                  </td>
               </tr>
          </div>
       </table>
   </form>
   <button type="button" class="btn btn-outline-success" (click)="DelBtn()"><span class="fa fa-plus"></span>Delete</button>
   </div>

现在在顶部的代码中,我们有一个添加按钮。然后我们有一个带有删除按钮的表单。我需要的是,当我单击添加按钮时,此表单应与删除按钮一起出现。然后当我单击删除按钮时,表单和删除按钮应该会消失。

另一件事是,如果我第二次单击添加按钮,我应该会得到一个表单以及删除按钮。对于每次点击,这应该重复。

【问题讨论】:

  • 您不需要任何方法,只使用 ngif* 指令和两个按钮,第一个按钮 ngif*="variable" 和第二个按钮 ngif*=!variable" - 表单的变量依赖是提交或不是
  • DelBtn() 的代码在哪里? HTML 很有用,但除非我们看到删除按钮的 JS,否则无法修复删除按钮的问题。
  • 我相信你需要看看*ngFor指令,你只需循环一个数组,删除和添加元素,这将自动反映在UI中
  • @WalterWhite 那件事我已经完成了,但我想在每次单击按钮时重复表单。我被困在那部分
  • @OwenKelvin 你能给我一个例子吗

标签: javascript html angular forms angular-material


【解决方案1】:

使用*ngFor,您可以循环遍历数组并在 UI 上显示内容 在 TS 文件中

  isformes = true;
  formsArray = [""];
  DelBtn = delIndex => this.formsArray.splice(delIndex, 1);
  AddBtn = () => this.formsArray.push("");

HTML 文件

<ng-container *ngFor="let i of formsArray; let j = index">
    <div *ngIf="isformes">
      <!-- Other staffs here -->
   </div>
</ng-container>

See Sample Link

【讨论】:

  • 您好,感谢您的帮助。有效。你能解释一下 DelBtn () 是如何工作的吗?
  • 您将索引传递给函数并从数组中删除具有该索引的项目,当数组中的项目更改时,Angular 将重新呈现 UI
【解决方案2】:

我建议创建一个包含输入字段和删除按钮的新表单组件,然后在用户单击添加按钮时动态加载该组件。

您需要使用 componentFactoryResolver,它将返回 componentFactory 并使用它您可以动态创建组件并使用 ng-template 将其添加到 UI 中。

addForm = () => {
    const componentFactory = this.componentFactoryResolver.resolveComponentFactory(
      FormComponent
    );
    const component = this.container.createComponent(componentFactory);
    component.instance.removeForm.subscribe((x) => {
      this.removeForm();
    });
    this.components.push(component);
  };

要删除您的组件,请在您的 formComponent 中公开一个输出参数,当用户单击您的父组件时,它将发出该事件,您可以在此处使用以下代码从 UI 中删除该组件。

 removeForm = () => {
    const component = this.components.find(
      (component) => component.instance instanceof FormComponent
    );
    const componentIndex = this.components.indexOf(component);

    if (componentIndex !== -1) {
      this.container.remove(this.container.indexOf(component));
      this.components.splice(componentIndex, 1);
    }
  };

这里是示例代码框:- https://codesandbox.io/s/pensive-butterfly-p8gl9?file=/src/index.html

【讨论】:

  • 嘿,这个很长。你能给我一些简短的例子吗
  • 这不是一个冗长的,这将满足您的要求以及您的应用程序的可扩展性。您是否有特定的限制计数用户可以添加多少表单?如果没有,我建议你走这条路。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-10
  • 2017-10-09
  • 1970-01-01
  • 2013-07-25
  • 1970-01-01
相关资源
最近更新 更多