Angular 6:如何在保存事件上绑定formArray的值?
创始人
2024-10-16 13:00:23
0

要在保存事件上绑定formArray的值,你可以使用Angular的Reactive Forms。下面是一个示例代码,展示了如何在Angular 6中实现这个功能:

  1. 首先,在组件类中导入相关的依赖项:
import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, FormArray } from '@angular/forms';
  1. 在组件类中定义一个表单组:
@Component({
  selector: 'app-my-component',
  templateUrl: './my-component.component.html',
  styleUrls: ['./my-component.component.css']
})
export class MyComponentComponent implements OnInit {
  myForm: FormGroup;

  constructor(private fb: FormBuilder) { }

  ngOnInit() {
    this.myForm = this.fb.group({
      items: this.fb.array([])
    });
  }

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

  addItem() {
    const item = this.fb.group({
      name: '',
      description: ''
    });

    this.itemForms.push(item);
  }

  deleteItem(index) {
    this.itemForms.removeAt(index);
  }

  save() {
    console.log(this.myForm.value);
  }
}
  1. 在模板中绑定表单组的值:

在这个示例中,我们创建了一个名为myForm的表单组,它包含一个名为items的formArray。addItem()方法用于向formArray中添加一个新的表单组,deleteItem()方法用于删除指定索引的表单组,save()方法用于在保存按钮点击时打印表单组的值。

希望这个示例能够帮助你实现在保存事件上绑定formArray的值。

相关内容

热门资讯

透视必备!werplan怎么透... 透视必备!werplan怎么透视(透视)hhpoker有透视的吗,教程指南书(真实有挂)-哔哩哔哩1...
hh poker插件下载!安装... hh poker插件下载!安装不了wepoker(透视)插件-都是揭露真的有挂1、下载好hh pok...
透视关于!hhpoker德州透... 透视关于!hhpoker德州透视(透视)hhpoker一直输有挂吗,教程步骤(有人有挂)-哔哩哔哩1...
aa poker透视软件!we... aa poker透视软件!wepoker辅助是真的吗(透视)app-都是透视有挂一、wepoker辅...
透视解密!红龙poker辅助工... 透视解密!红龙poker辅助工具(透视)wepoker能不能透视,教程教程书(有挂辅助)-哔哩哔哩1...
wepoker辅助器有哪些功能... wepoker辅助器有哪些功能!wepoker透视功能下载(透视)软件-总是关于是真的挂1、wepo...
透视了解!拱趴大菠萝有什么挂(... 透视了解!拱趴大菠萝有什么挂(透视)aa poker辅助包,教程方式(有人有挂)-哔哩哔哩1、这是跨...
智星德州可以透视吗!wepok... 智星德州可以透视吗!wepoker私人局规律(透视)挂-一贯科普真的是有挂1、每一步都需要思考,不同...
透视曝光!wejoker辅助机... 透视曝光!wejoker辅助机器人(透视)pokemmo脚本辅助器下载,教程要领(了解有挂)-哔哩哔...
fishpoker透视!aap... fishpoker透视!aapoker怎么设置提高好牌几率(透视)挂-都是曝光是真的挂该软件可以轻松...