[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);
}
[Angular] Working with FormArray的更多相关文章
- [Angular] Update FormArray with patchValue
Currently, patchValue doesn't support update FormArray. The workarround is you need to empty the for ...
- Angular之响应式表单 ( Reactive Forms )
项目结构 一 首页 ( index.html ) <!doctype html> <html lang="en"> <head> <met ...
- Angular使用总结 --- 模型驱动表单
模型驱动表单 之前有篇博文总结了 模版驱动表单 , 以及 模版驱动表单的自定义校验 , 本篇总结下模型驱动表单. 与模版驱动表单是不同的编程思路,偏向于数据模型.先在组件中建立表单控件的对象树,再绑定 ...
- Angular 2 + 折腾记 :(7) 初步了解表单:模板驱动及数据驱动及脱坑要点
前言 表单在整个系统中的作用相当重要,这里主要扯下响应表单的实现方式. 首先须要操作表单的模块引入这两个模块. import { FormsModule, ReactiveFormsModule } ...
- Angular : 响应式编程, 组件间通信, 表单
Angular 响应式编程相关 ------------------------------------------------------------------------------------ ...
- [Angular] Component architecture and Reactive Forms
It it recommeded that when deals with form component, we can create a container component to hold st ...
- [Angular] Test Container component with async provider
The main idea for testing contianer component is to make sure it setup everythings correctlly. Call ...
- 表单-angular
模板表单: <form #myform="ngForm" (ngSubmit)="onsubmit(myform.value)" > <div ...
- Angular表单 (一)表单简介
Angular 表单 angular提供了两种不同的方法来通过表单处理用户输入:响应式表单和模板驱动表单.二者都从视图中捕获用户输入事件.验证用户输入.创建表单模型.修改数据模型,并提供跟踪这些更改的 ...
随机推荐
- 简单的横向ListView实现(version 4.0)
这个版本号的博客写起来颇费口舌.有些代码自己语言组织能力有限,感觉描写叙述起来非常费劲,前前后后改了五六遍稿子还是不尽人意 ,只是我还是坚持写出来自己当初的思路,假设看得不明确的地方我在文章最后仍然会 ...
- HDU 5389 Zero Escape (MUT#8 dp优化)
[题目链接]:pid=5389">click here~~ [题目大意]: 题意: 给出n个人的id,有两个门,每一个门有一个标号,我们记作a和b,如今我们要将n个人分成两组,进入两个 ...
- COOKIE传值
1.遍历所有cookie foreach (string _key in Request.Cookies.AllKeys)//遍历主键 { Htt ...
- AAC编解码
AAC编码可以使用faac /** 初始化 @param sampleRate 音频采样率 @param channels 通道数 @param bitSize 音频采样精度 16 */ - (voi ...
- How to remove a Data Guard Configuration from Primary Database (文档 ID 733794.1)
APPLIES TO: Oracle Database - Enterprise Edition - Version 10.1.0.2 to 11.2.0.3 [Release 10.1 to 11. ...
- TC Hangs when using quick search extended on win10 (1703)
https://ghisler.ch/board/viewtopic.php?t=47682 I recently updated windows 10 to the latest released ...
- windows CE项目开发
软件列表 1.Windows mobile 设备中心 2.Microsoft visual Studio 2008 3.串口调试工具(sscom42.exe) 4.Wince 6.0模拟器 5.vir ...
- 【例题 7-5 UVA - 129】Krypton Factor
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 每次枚举增加一个字符; 然后看看新生成的字符的后缀里面有没有出现连续子串就好,前面已经确认过的没必要重复确认 (枚举长度为偶数的一个 ...
- 【2017中国大学生程序设计竞赛 - 网络选拔赛 hdu 6150】Vertex Cover
[链接]点击打开链接 [题意] 有人写了一个最小点覆盖的贪心算法,然后,让你去hack它. 并且,要求这个算法得到的错误答案,是正确答案的三倍. 让你任意输出hack数据,点数<=500 [题解 ...
- stm32单片机下载方式
引用 编辑:什么鱼 引用地址:http://www.eeworld.com.cn/mcu/2015/1012/article_22873.html 第一种 ISP下载: 这里类似51. boot1 ...