[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 ...
随机推荐
- 【2017中国大学生程序设计竞赛 - 网络选拔赛】Palindrome Function
[链接]http://acm.hdu.edu.cn/showproblem.php?pid=6156 [题意] 已知函数f(x, k),如果10进制数x在k进制下是个回文数,那么f(x, k)值为k, ...
- Mining Station on the Sea (hdu 2448 SPFA+KM)
Mining Station on the Sea Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Jav ...
- Android中使用HttpClient实现HTTP通信效果
HTTP通信,这一案例在操作的时候遇到N多种种问题,是前面看过几个实例里面最麻烦的一个.由于没有系统的接触过JAVA,所以出了非常多错误,也无从下手解决,这里经过对错误的检索实现了HTTP通信,以做记 ...
- android.mk-编译文件学习(转载)
工作了那么久,都是使用大神们写的脚本机械的编译,对于android.mk根本没去了解过.今天趁着这个机会,在网上搜索了下.虽然依然不是很名白,留做记录,以后真用到了,再深入研究 转载自 http:// ...
- eclipse-hierarchyviewer 不能使用
今天安装了adt-bundle以后,发现hierarchyviewer不能用.点开了以后连手机没有效果.后来发现,还需要进入hierarchyviewer所在的sdk目录进行下权限的设置 chmod ...
- background-size在PC端和移动端使用媒体查询的不同
1.PC端background-size:100%:是展现原图的大小. 2.使用媒体查询的移动端的background-size:100%:是根据内容的高度自动拉伸高度的.
- jquery点击完一个按钮,并且触发另一个按钮
$a.click(function(){ $b.trigger('click'); });
- 【前端切图】用css画一个卡通形象-小猪佩奇
最近在腾讯云技术社区遇到了一位奇才,用css画出了一个社会人小猪佩奇,不得不服.研究了一下他的文章https://segmentfault.com/a/1190000014909658,感觉甚是有趣, ...
- oracle-function 练习
/* *scm_iss.test_imti_fun2 *带有输入參数的Function */ CREATE OR REPLACE FUNCTION TEST_IMTI_FUN2(P_NO IN NUM ...
- WebApp调用手机相册或摄像头、拨打电话
WebApp调用手机相册或摄像头.拨打电话 一.总结 一句话总结:input标签,指定type为file,选择好对应的accept即可.camera——相机,相应的accept为image : cam ...