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

  1. angular reactive form

    这篇文章讲了angular reactive form, 这里是angular file upload 组件 https://malcoded.com/posts/angular-file-uploa ...

  2. Angular Reactive Form - 填充表单模型

    setValue 使用setValue,可以通过传递其属性与FormGroup后面的表单模型完全匹配的数据对象来一次分配每个表单控件值. 在分配任何表单控件值之前,setValue方法会彻底检查数据对 ...

  3. Angular:Reactive Form的使用方法和自定义验证器

    本文将介绍Angular(Angular2+)中Reactive Form的有关内容,包括: Reactive Form创建方法 如何使用验证 自定义验证器 下面开始进入正文! Reactive Fo ...

  4. Angular Reactive Forms -- Model-Driven Forms响应式表单

    Angular 4.x 中有两种表单: Template-Driven Forms - 模板驱动式表单 (类似于 AngularJS 1.x 中的表单 )  官方文档:https://v2.angul ...

  5. [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 ...

  6. 4.bootstrap的form表单的form-group和form-control的区别与联系

    1. form-group一般用于div form-control一般用于置于div中的标签元素,为了让控件在各种表单风格中样式不出错,需要添加类名“form-control”,如: <form ...

  7. [Angular2 Form] Reactive Form, show error message for one field

    <form [formGroup]="reactiveForm" novalidate autocomplete="off"> <div cl ...

  8. angular实现form验证

    先上效果页面:https://lpdong.github.io/myForm-1/ 其中几个知识点 1.angularJs提供了几个新的type类型: type="password" ...

  9. [Angular2 Form] Reactive Form, FormBuilder

    Import module: import { NgModule } from '@angular/core'; import { CommonModule } from '@angular/comm ...

随机推荐

  1. 【Educational Codeforces Round 35 D】Inversion Counting

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 排列中交换任意两个数字. 排列的逆序对个数的奇偶性会发生变化. 翻转这个过程其实就是len/2对数字发生交换. 交换了偶数次的话,不 ...

  2. ubuntu-安装中文拼音输入法

    一下内容转载自http://blog.chinaunix.net/uid-24410388-id-3501873.html 自己验证了可用.转载了,已留做日后使用 步骤: step1:安装ibus所需 ...

  3. 使用@Order调整配置类加载顺序

    转自:https://blog.csdn.net/qq_15037231/article/details/78158553 4.1 @Order Spring 4.2 利用@Order控制配置类的加载 ...

  4. 微信消息体加解密及EncodingAESKey

    公众平台消息体签名及加解密方案概述 1.新增消息体签名验证,用于公众平台和公众账号验证消息体的正确性 2.针对推送给微信公众账号的普通消息和事件消息,以及推送给设备公众账号的设备消息进行加密 3.公众 ...

  5. vue实现一个会员卡的组件(可以动态传入图片(分出的一个组件)、背景、文字、卡号等)

    自己在写这个组件的时候主要遇到的问题就是在动态传入背景图片或者背景色的时候没能立马顺利写出来,不过现在实现了这个简单组件就和大家分享一下 <template> <div class= ...

  6. VC++的函数指针和回调函数 及友元函数

    什么是函数指针 函数指针是指向函数的指针变量.也就是说,它是一个指针变量,而且该指针指向一个函数. 对于指针变量来说,它的值是它指向的变量的地址.举个例子:指针变量pi是指向一个整型变量i的指针,则变 ...

  7. vs2015 EF code first 问题待解决

    在vs 2013 上可以成功ef 生成代码.EF power Tools 安装在vs 2015 :一般不可安装, 把扩展名改成zip,解压缩. 打开extension.vsixmanifest文件 找 ...

  8. iOS开发之CocoaPods(objective-c第三方库管理工具)

    介绍: iOS开发中,大多数情况下,我们都须要集成一些第三方依赖库.对于一个稍大的项目,用到的第三方依赖库的数量也很可观.CocoaPods是objective-c第三方库管理工具,方便第三方库的管理 ...

  9. Altium Designer一些问题

    1,当你重新导入原理图的时候,会提示删除类 2,不要粘贴和赋值pcb中的原件,否则飞线可能会消失

  10. 去哪网实习总结:用到的easyui组件总结(JavaWeb)

    本来是以做数据挖掘的目的进去哪网的,结构却成了系统开发... 只是还是比較认真的做了三个月,老师非常认同我的工作态度和成果.. . 实习立即就要结束了,总结一下几点之前没有注意过的变成习惯和问题,分享 ...