[Angular] Working with FormArray
'FormArray' can work with array of 'FormGroup' or 'FormControl'.
form = new FormGroup({ stock: new FormArray([ new FormGroup({ product_id: new FormControl(1), quantity: new FormControl(10) }), new FormGroup({ product_id: new FormControl(12), quantity: new FormControl(90) }), ]) });
So for Javascript, we have a 'form=new FormGroup()', inside formGoup, we have a 'stock' formArray with multi formGroup inside.
In the html, we use component approach:
<stock-products (removed)="removeStock($event)" [parent]="form"> </stock-products>
We create a custom component.
Inside the component:
import { Component, Input } from '@angular/core'; import { FormGroup, FormArray } from '@angular/forms'; @Component({ selector: 'stock-products', styleUrls: ['stock-products.component.scss'], template: ` <div class="stock-product" [formGroup]="parent"> <div formArrayName="stock"> <div *ngFor="let item of stocks; let i = index;"> <div class="stock-product__content" [formGroupName]="i"> <div class="stock-product__name"> {{ item.value.product_id }} </div> <input type="number" step="10" min="10" max="1000" formControlName="quantity"> <button type="button"> Remove </button> </div> </div> </div> </div> ` }) export class StockProductsComponent { @Input() parent: FormGroup; get stocks() { return (this.parent.get('stock') as FormArray).controls; } }
First:
<div class="stock-product" [formGroup]="parent">
Tells what want to bind 'form' object passing down from the parent component.
Then to keep the same structure, we add:
<div formArrayName="stock">
Because "stock" is a FormArray.
Inside the Form array, we loop thought each form group:
<div class="stock-product__content" [formGroupName]="i">
We use the index as key to bind 'formGroupName', it actually feeling the same as work with JS array, accessing by the index.
Notice how we get 'stock' from 'form':
get stocks() { return (this.parent.get('stock') as FormArray).controls; }
Using
(this.parent.get('stock') as FormArray)
just to tell Typescrpt, this is a FormArray type.
The same as :
get stocks() { const ary = <FormArray>this.parent.get('stock'); return ary.controls; }
To add or remove from the FormArray: we have 'push' and 'removeAt' methods
addStock(stock) { const control = this.form.get('stock') as FormArray; control.push(this.createStock(stock)); } removeStock({ group, index }: { group: FormGroup, index: number }) { const control = this.form.get('stock') as FormArray; control.removeAt(index); }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2016-03-21 [Angular 2] Using Array ...spread to enforce Pipe immutability
2016-03-21 [Angular 2] Using Pipes to Filter Data
2016-03-21 [Angular 2] Controlling how Styles are Shared with View Encapsulation
2016-03-21 [Angular 2]ng-class and Encapsulated Component Style2
2016-03-21 [Angular 2] Passing data to components with @Input
2016-03-21 [Angular 2] Template property syntax
2016-03-21 [Angular 2] Adding a data model