Angular响应式表单FormArray - 添加按钮打开另一个弹出表单
创始人
2024-10-30 06:30:43
0

下面是一个使用Angular响应式表单FormArray的示例,其中包含一个按钮,当点击按钮时,会打开另一个弹出表单:

首先,定义一个包含表单控件的FormArray:

import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, FormArray } from '@angular/forms';

@Component({
  selector: 'app-form',
  templateUrl: './form.component.html',
  styleUrls: ['./form.component.css']
})
export class FormComponent implements OnInit {
  myForm: FormGroup;

  constructor(private fb: FormBuilder) { }

  ngOnInit() {
    this.myForm = this.fb.group({
      items: this.fb.array([]) // 创建一个空的FormArray
    });
  }

  get items(): FormArray {
    return this.myForm.get('items') as FormArray;
  }

  addItem() {
    // 向FormArray中添加新的FormGroup
    const newItem = this.fb.group({
      name: '',
      age: ''
    });
    this.items.push(newItem);
  }

  removeItem(index: number) {
    // 从FormArray中移除指定的FormGroup
    this.items.removeAt(index);
  }
}

然后,在HTML模板中,通过ngFor循环来显示FormArray中的每个FormGroup,并在表单中添加一个按钮来打开另一个弹出表单:

这样,当点击"Add Item"按钮时,会在表单中添加一个新的FormGroup,并显示一个新的输入字段。

相关内容

热门资讯

透视ai!wepoker透视方... 透视ai!wepoker透视方法,wepoker软件靠谱么(详细有人玩)1、wepoker软件靠谱么...
透视软件!aapoker万能辅... 透视软件!aapoker万能辅助器(透视)aapoker辅助工具安全吗,系统教程(有挂攻略)1、aa...
透视辅助!wepoker好友局... 透视辅助!wepoker好友局透视,wepoker有脚本吗(详细h5破解)1、完成wepoker好友...
透视系统!aapoker万能辅... 透视系统!aapoker万能辅助器(透视)aapoker ai插件,详细教程(有挂解说)1、该软件可...
透视有挂!wepoker作弊辅... 透视有挂!wepoker作弊辅助,wepoker辅助器安装包定制(详细辅助器要钱玩)1、wepoke...
透视智能ai!aapoker透... 透视智能ai!aapoker透视脚本入口(透视)aapoker辅助怎么用,详细教程(有挂解说)该软件...
透视软件!wepoker破解工... 透视软件!wepoker破解工具,wepoker作弊视频(详细轻量版透视方法)1、点击下载安装,we...
透视数据!aapoker透视方... 透视数据!aapoker透视方法(透视)aapoker辅助器是真的吗,详细教程(有挂技巧)1、下载好...
透视真的!wepoker怎么挂... 透视真的!wepoker怎么挂飞机,wejoker辅助机器人(详细有人玩)1、完成wepoker怎么...
透视系统!aapoker真的假... 透视系统!aapoker真的假的(透视)aa poker透视软件,新版2025教程(有挂攻略);该软...