[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 ...
随机推荐
- Boost解析xml——xml写入
<?xml version="1.0" encoding="utf-8"?> <Config> <Item name=" ...
- Relaxation step(Dijkstra's 最短路径算法)
翻译成中文就是"松弛",属于工程优化的范畴: Dijkstra 的单源最短路径算法,有一个重要的步奏,当访问到新的结点 u (加入到集合 S),然后遍历 u 的邻接顶点(Adj), ...
- Writing Images to the Excel Sheet using PHPExcel--转载
原文地址:http://www.walkswithme.net/writing-images-to-the-excel-sheet-using-phpexcel Writing images to t ...
- oracle里long类型的总结
转自原文oracle中long类型为什么不推荐使用 不是不推荐使用的,是一般用不到,而有些时候是会用到的,所以不能一概而论.1.LONG 数据类型中存储的是可变长字符串,最大长度限制是2GB.2.对于 ...
- Lintcode 将整数A转换为B
例子 如把31转换为14,须要改变2个bit位. ()10=()2 ()10=()2 贴代码 class Solution { public: /** *@param a, b: Two intege ...
- Android 使用JSON格式与服务器交互 中文乱码问题解决
当前是在开发Android 程序时,客户端与服务器端采用JSON传送数据,发现中文乱码问题.不过这个问题的解决办法应该对所有java语言开发的项目都使用. 解决方法是: 1.客户端发送数据之间加上: ...
- HDU 1166 敌兵布阵 树状数组||线段树
http://acm.hdu.edu.cn/showproblem.php?pid=1166 题目大意: 给定n个数的区间N<=50000,还有Q个询问(Q<=40000)求区间和. 每个 ...
- IOS获取手机设备所有应用
//返回一个数组 1 NSMutableArray *applist = [[NSMutableArray alloc]init]; NSString *pathOfApplications = @& ...
- 每日技术总结:setInterval,setTimeout,文本溢出,小程序,wepy
前言: 项目背景:vue,电商,商品详情页 1.倒计时,倒计到0秒时停止 data () { return { n: 10 } }, created () { let int = setInterva ...
- 5、linux下应用字符串相关调用函数列举说明
1.函数原型int strcmp(const char *s1,const char *s2);设这两个字符串为s1,s2,规则当s1<s2时,返回为负数当s1=s2时,返回值= 0当s1> ...