[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=" ...
- 【Codeforces Round #452 (Div. 2) D】Shovel Sale
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 让N乘2->a 然后看一下位数是多少. 假设有x位(x>=2) 则(0..(a%10-1) ) + (99..9)[x- ...
- [Node & Testing] Intergration Testing with Node Express
We have express app: import _ from 'lodash' import faker from 'faker' import express from 'express' ...
- Activity启动模式的深入分析
网上关于Activity启动模式的文章许多.可是看起来都千篇一律,看完之后我们都能理解这4种启动模式.只是官方api对singleTask这个启动模式解释有些争议,导致我事实上并没有真正理解这几种模式 ...
- Bitmap-把方形图片处理为圆形
这个是直接在网上转载的,自己验证可靠 转载自http://my.oschina.net/zhouz/blog/213164 直接贴上代码 import android.graphics.Bitmap; ...
- Flask项目之手机端租房网站的实战开发(七)
说明:该篇博客是博主一字一码编写的,实属不易,请尊重原创,谢谢大家! 接着上一篇博客继续往下写 :https://blog.csdn.net/qq_41782425/article/details/8 ...
- ArcGIS教程:地理处理服务演示样例(河流网络)(三)
设置输出符号系统 步骤: 展开 StoweStreamNet.tbx 并双击创建河流网络模型. 接受默认的 45 公顷并单击确定以运行模型. StreamNet 图层将加入至 ArcMap. 右键单击 ...
- SQL server 错误代码对比表
0 操作成功完毕. 1 功能错误. 2 系统找不到指定的文件. 3 系统找不到指定的路径. 4 系统无法打开文件. 5 拒绝訪问. 6 句柄无效. ...
- Android实践 -- 对apk进行系统签名
对apk进行系统签名 签名工具 网盘下载 ,需要Android系统的签名的文件 platform.x509.pem 和 platform.pk8 这个两个文件在Android源码中的 ./build/ ...
- 关于Newtonsoft.json JsonConvert.DeserializeObject反序列化的使用
object obj = JsonConvert.DeserializeObject("{\"Sta\":3}", paramClass); //paramCl ...