操作Form Array (实用)

博客提供了Angular学习相关参考内容,涉及Angular响应式表单中FormArray与FormGroup相互嵌套的学习笔记,以及angular4 Form Array用patchValue增加记录、替换值的文章链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先上代码:

import { FormBuilder, Validators, FormArray, FormGroup, FormControl } from '@angular/forms';

protected createForm() {
    this.form = this.formBuilder.group({
        ID: [""],
        ItemTypeID: ["", Validators.required],
        StdPrice: [""],
        SysDefine: ["0"],
        Remark: [""],
        ...
        // 定义From Array 
        ItemSpecs: this.formBuilder.array([
            new FormGroup({
                SpecName: new FormControl(null),
                Price: new FormControl(null),
                StdPrice: new FormControl(null),
            }),
        ]),
    });
    ...
}

 {
        // 找出 Form Array
        let itemSpecs = this.form.controls['ItemSpecs'] as FormArray;
        
        // 修改记录
        specItems.map((item, index) => {
 	    itemSpecs.at(index).patchValue({
                SpecName: item.SpecName,
                Price: Number(item.Price),
                StdPrice: Number(item.StdPrice),
           })
        });
       
        // 增加记录
        specItems.push({
            SpecName: item.SpecName,
            Price: Number(item.Price),
            StdPrice: Number(item.StdPrice),
        });        
}

参考:
1、Angular学习笔记24:Angular 响应式表单 FormArray 与 FormGroup 相互嵌套
      https://blog.youkuaiyun.com/wjyyhhxit/article/details/84673328     (定义From Array)

2、angular4 Form Array https://www.jianshu.com/p/40d22d760f93  (用patchValue 增加记录、替换值)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值