[Angular] Reactive Form -- FormControl & formControlName, FormGroup, formGroup & formGroupName
First time dealing with Reactive form might be a little bit hard to understand.
I have used Angular-formly for AngularJS bofore, what it does is using Javascript to define form's template, data and validations. In HTML, it is just a simple directive with some bindings.
<form>
<angular-formly model="$ctrl.model" options="$ctrl.options" form="$ctrl.form" fields="$ctrl.fields"></angular-formly>
</form>
So what is the main difference between Angular Formly and Reactive Form for Angular (v >= 2.0)?
Well, the main difference is that "Reactive form return the template part to the html, only take care of data and validations". So what it means is that when you use Reactive form, you still need to structure what your form should looks like by using HTML. But data bindings and validations will be taken over by Angular.
So what is the benenfits by doing "return template part to the html"? Well this allows user passing custom components and bind those components to the reactive form really easily.
Of course, in Angular Formly you also able to define your custom "template", but it is not so easy, it requires you to know the Formly APIs -- how to define a custom template. But still Angular Formly is really good libaray for AngularJS to deal with complex form logic, a much cleaner and nicer way than control all the stuffs (template, error messages, validations...) by html.
So let's take an example first before explain some details stuff:
import { Component } from '@angular/core';
import { FormControl, FormGroup } from '@angular/forms'; @Component({
...
template: `
<div class="stock-inventory">
<form [formGroup]="form"> <div formGroupName="store">
<input
type="text"
placeholder="Branch ID"
formControlName="branch">
<input
type="text"
placeholder="Manager Code"
formControlName="code">
</div> </form>
</div>
`
})
export class StockInventoryComponent {
form = new FormGroup({
store: new FormGroup({
branch: new FormControl('B182'),
code: new FormControl('1234')
})
})
}
In the html, we have '[formGroup]', 'formGroupName', 'formControlName'.
The structure is like:
[formGroup]="form"
----formGroupName="store"
--------formControlName="branch"
--------formControlName="code"
In the Javascript, we define:
form = new FormGroup({
store: new FormGroup({
branch: new FormControl('B182'),
code: new FormControl('1234')
})
})
The structure is like:
FormGroup="form"
----FormGroup="store"
--------FormControl="branch"
--------FormControl="code"
So you can find that the html and JS code structure is the same. Just one question you may ask why:
<form [formGroup]="form">
and
<div formGroupName="store">
One use [formGroup] and the other use "formGroupName"?
Well, the answer is really simple, because "[formGroup]=form", this "form" is an JS object. "store" is just an prop on the "form" object, so ALL the props-like stuff, we add "xxxName", like "formGroupName" and "formControlName".
Now Angular is ready to take over the data bindings for the form.
Last thing I want to memtion in this post is passing "custom component" to the form.
So how to do that? Take previous example, we convert:
<div formGroupName="store">
<input
type="text"
placeholder="Branch ID"
formControlName="branch">
<input
type="text"
placeholder="Manager Code"
formControlName="code">
</div>
To a component.
Create a new component:
import { Component, Input } from '@angular/core';
import { FormGroup } from '@angular/forms'; @Component({
selector: 'stock-branch',
styleUrls: ['stock-branch.component.scss'],
template: `
<div [formGroup]="parent">
<div formGroupName="store">
<input
type="text"
placeholder="Branch ID"
formControlName="branch">
<input
type="text"
placeholder="Manager Code"
formControlName="code">
</div>
</div>
`
})
export class StockBranchComponent {
@Input()
parent: FormGroup;
}
We copy the html to the new component, we only add a extra Input "parent". So what it is ?
import { Component } from '@angular/core';
import { FormControl, FormGroup } from '@angular/forms'; @Component({
...
template: `
<div class="stock-inventory">
<form [formGroup]="form"> <stock-branch
[parent]="form">
</stock-branch> </form>
</div>
`
})
export class StockInventoryComponent {
form = new FormGroup({
store: new FormGroup({
branch: new FormControl(''),
code: new FormControl('')
})
})
}
As you can see, we actually just pass down "form" object down to the child component. As re-structure [formGroup]-formGroupName-formControlName in the new component.
So by now, hope it is clear for you how easy it is for Reactive form binding form to custom component. And hope you already find another tips: the chain partten: '[formGroup]-->formGroupName-->formControlName'
[Angular] Reactive Form -- FormControl & formControlName, FormGroup, formGroup & formGroupName的更多相关文章
- angular reactive form
这篇文章讲了angular reactive form, 这里是angular file upload 组件 https://malcoded.com/posts/angular-file-uploa ...
- Angular Reactive Form - 填充表单模型
setValue 使用setValue,可以通过传递其属性与FormGroup后面的表单模型完全匹配的数据对象来一次分配每个表单控件值. 在分配任何表单控件值之前,setValue方法会彻底检查数据对 ...
- Angular:Reactive Form的使用方法和自定义验证器
本文将介绍Angular(Angular2+)中Reactive Form的有关内容,包括: Reactive Form创建方法 如何使用验证 自定义验证器 下面开始进入正文! Reactive Fo ...
- Angular Reactive Forms -- Model-Driven Forms响应式表单
Angular 4.x 中有两种表单: Template-Driven Forms - 模板驱动式表单 (类似于 AngularJS 1.x 中的表单 ) 官方文档:https://v2.angul ...
- [Angular2 Form] Reactive form: valueChanges, update data model only when form is valid
For each formBuild, formControl, formGroup they all have 'valueChanges' prop, which is an Observable ...
- 4.bootstrap的form表单的form-group和form-control的区别与联系
1. form-group一般用于div form-control一般用于置于div中的标签元素,为了让控件在各种表单风格中样式不出错,需要添加类名“form-control”,如: <form ...
- [Angular2 Form] Reactive Form, show error message for one field
<form [formGroup]="reactiveForm" novalidate autocomplete="off"> <div cl ...
- angular实现form验证
先上效果页面:https://lpdong.github.io/myForm-1/ 其中几个知识点 1.angularJs提供了几个新的type类型: type="password" ...
- [Angular2 Form] Reactive Form, FormBuilder
Import module: import { NgModule } from '@angular/core'; import { CommonModule } from '@angular/comm ...
随机推荐
- 【Codeforces Round #451 (Div. 2) B】Proper Nutrition
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 可以直接一层循环枚举. 也可以像我这样用一个数组来存y*b有哪些. 当然.感觉这样做写麻烦了.. [代码] /* 1.Shoud i ...
- 洛谷 P2694 接金币
P2694 接金币 题目描述 在二维坐标系里,有N个金币,编号0至N-1.初始时,第i个金币的坐标是(Xi,Yi).所有的金币每秒向下垂直下降一个单位高度,例如有个金币当前坐标是(xf, yf),那么 ...
- [React] Call setState with null to Avoid Triggering an Update in React 16
Sometimes it’s desired to decide within an updater function if an update to re-render should be trig ...
- Spring入门--控制反转(IOC)与依赖注入(DI)
1.控制反转(Inversion of Control)与依赖注入(Dependency Injection) 控制反转即IoC (Inversion of Control).它把传统上由程序 ...
- 九度OJ—题目1032:ZOJ
题目描写叙述: 读入一个字符串.字符串中包括ZOJ三个字符,个数不一定相等,按ZOJ的顺序输出.当某个字符用完时,剩下的仍然依照ZOJ的顺序输出. 输入: 题目包括多组用例,每组用例占一行,包括ZOJ ...
- Flume Interceptors官网剖析(博主推荐)
不多说,直接上干货! Flume Sources官网剖析(博主推荐) Flume Channels官网剖析(博主推荐) Flume Channel Selectors官网剖析(博主推荐) Flume ...
- Flask项目之手机端租房网站的实战开发(五)
说明:该篇博客是博主一字一码编写的,实属不易,请尊重原创,谢谢大家! 接着上一篇博客继续往下写 :https://blog.csdn.net/qq_41782425/article/details/8 ...
- VC++ 6.0 BUG BUG BUG BUG BUG
http://blog.163.com/amao831@126/blog/#m=0 我经常在的VC++6.0中 定义某个类的对象时 再用.访问或者->访问时不自动弹出他的成员函数或者成员变量 最 ...
- 洛谷—— P1069 细胞分裂
https://www.luogu.org/problem/show?pid=1069#sub 题目描述 Hanks 博士是 BT (Bio-Tech,生物技术) 领域的知名专家.现在,他正在为一个细 ...
- CSS两列布局——左侧宽度固定,右侧宽度自适应的3种方法
1.左侧绝对定位法 直接看代码: <!DOCTYPE html> <html lang="en"> <head> <meta charse ...