'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的更多相关文章

  1. [Angular] Update FormArray with patchValue

    Currently, patchValue doesn't support update FormArray. The workarround is you need to empty the for ...

  2. Angular之响应式表单 ( Reactive Forms )

    项目结构 一 首页 ( index.html ) <!doctype html> <html lang="en"> <head> <met ...

  3. Angular使用总结 --- 模型驱动表单

    模型驱动表单 之前有篇博文总结了 模版驱动表单 , 以及 模版驱动表单的自定义校验 , 本篇总结下模型驱动表单. 与模版驱动表单是不同的编程思路,偏向于数据模型.先在组件中建立表单控件的对象树,再绑定 ...

  4. Angular 2 + 折腾记 :(7) 初步了解表单:模板驱动及数据驱动及脱坑要点

    前言 表单在整个系统中的作用相当重要,这里主要扯下响应表单的实现方式. 首先须要操作表单的模块引入这两个模块. import { FormsModule, ReactiveFormsModule } ...

  5. Angular : 响应式编程, 组件间通信, 表单

    Angular 响应式编程相关 ------------------------------------------------------------------------------------ ...

  6. [Angular] Component architecture and Reactive Forms

    It it recommeded that when deals with form component, we can create a container component to hold st ...

  7. [Angular] Test Container component with async provider

    The main idea for testing contianer component is to make sure it setup everythings correctlly. Call ...

  8. 表单-angular

    模板表单: <form #myform="ngForm" (ngSubmit)="onsubmit(myform.value)" > <div ...

  9. Angular表单 (一)表单简介

    Angular 表单 angular提供了两种不同的方法来通过表单处理用户输入:响应式表单和模板驱动表单.二者都从视图中捕获用户输入事件.验证用户输入.创建表单模型.修改数据模型,并提供跟踪这些更改的 ...

随机推荐

  1. 3lession-python编程规范

    今天继续学习python,内容主要就是python编程过程中的一些规范,包括注释.换行等 1.注释 所有的注释都是以#开头,注释可以单独占有一行,也可以放到语句的末尾.因为python没有多行注释,所 ...

  2. 给已有数据的oracle表建立外键关系

    PS:这里是给自己做个备忘,下次遇到同类问题的时候,方便查找: 客户在有主外键关系的2张表进行页面删除时报错已有子记录,运维后台处理的时候应该找出相应的数据,先删除子记录,在删主表记录:但客户要的急, ...

  3. Centos7安装.Net Core 2.2环境以及部署.Net Core MVC程序(Apache+Jexus环境)

    原文:Centos7安装.Net Core 2.2环境以及部署.Net Core MVC程序(Apache+Jexus环境) 1.双11抢购***VPS.配置如下: CPU:2 核 内存:2048 M ...

  4. COGS——C66. [HAOI2004模拟] 数列问题

    http://www.cogs.pro/cogs/problem/problem.php?pid=66 ★☆   输入文件:dfs3.in   输出文件:dfs3.out   简单对比 时间限制:1 ...

  5. 解决Keystore was tampered with, or password was incorrect

    使用签名文件keystore查看生成的数字签名中报错解决 Keystore was tampered with, or password was incorrect 这是由于android规定自己定义 ...

  6. __INLINE

  7. qemu 参数简介

    参数 示例 说明 -hda -hda /data/windows.img 指定windows.img作为硬盘镜像 -cdrom -cdrom /data/windows.iso 指定windows.i ...

  8. APP功能点测试

    一.移动互联网特点: 1,用户体验至上:精准的用户体验 2,核心竞争力:市场占有率和业务创新能力 3,营销模型:通过口碑传播吸引客户,随之参与互动(如分享等,对接口测试要求高) 二.项目特点: 1,开 ...

  9. StackExchange.Redis 官方文档(六) PipelinesMultiplexers

    原文:StackExchange.Redis 官方文档(六) PipelinesMultiplexers 流水线和复用 糟糕的时间浪费.现代的计算机以惊人的速度产生大量的数据,而且高速网络通道(通常在 ...

  10. [TypeScript] Distinguishing between types of Strings in TypeScript

    In JavaScript, many libraries use string arguments to change behavior. In this lesson we learn how T ...