We can nest formGorup:

    this.reactiveForm = fb.group({
username: [
'',
[
Validators.required,
Validators.minLength()
]
],
pwds: fb.group({
pwd: '',
rpwd: ''
}, {validator: passwordValidator})
});

We make password as an own group. So in html, we need to use formGroupName istead of formControlName.

<form [formGroup]="reactiveForm" novalidate autocomplete="off">
<div class="form-field">
<label>Username:</label>
<input formControlName="username">
<div class="field-error-message" *ngIf="reactiveForm.controls.title.errors?.required">
Username is required
</div>
</div> <div formGroupName="pwds">
<div class="form-field">
<label>pwd</label>
<input formControlName="pwd">
</div>
<div class="form-field">
<label>rpwd</label>
<input formControlName="rpwd">
</div>
</div>
</form>

And how we check the value or errors?:

<pre>
{{reactiveForm.get('pwds')?.value | json}}
{{reactiveForm.get('pwds')?.errors | json}}
</pre>

And we also passwordValidator haven't cover yet, it is just a fucntion:

function passwordValidator(c: AbstractControl){
return c.get('pwd').value === c.get('rpwd').value ?
null : // valid
{ //invalid
nomatch: true
}
}

And notice that we put this validator inside the nested group, so we can get nice error effect:

[Angular2 Form] Nested formGroup, and usage of formGroupName的更多相关文章

  1. [Angular2 Form] Create custom form component using Control Value Accessor

    //switch-control component import { Component } from '@angular/core'; import { ControlValueAccessor, ...

  2. [Angular2 Form] Use RxJS Streams with Angular 2 Forms

    Angular 2 forms provide RxJS streams for you to work with the data and validity as it flows out of t ...

  3. [Angular2 Form] Reactive Form, FormBuilder

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

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

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

  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. [Angular2 Form] Check password match

    Learn how to create a custom validator to check whether passwords match. <h1>password match< ...

  7. [Angular2 Form] patchValue, setValue and reset() for Form

    Learn how to update part of form model, full form model and reset whole form. We have form definetio ...

  8. [Angular2 Form] Build Select Dropdowns for Angular 2 Forms

    Select Dropdowns in Angular 2 a built with select and option elements. You use *ngFor to loop throug ...

  9. [Angular2 Form] Create Radio Buttons for Angular 2 Forms

    Using Radio Buttons in Angular 2 requires a basic understanding of forms as well as how their labels ...

随机推荐

  1. php开启openssl扩展

    windows下开启方法: 1: 首先检查php.ini中:extension=php_openssl.dll是否存在, 如果存在的话去掉前面的注释符‘:’, 如果不存在这行,那么添加extensio ...

  2. command---调用指定的指令并执行

    command命令调用指定的指令并执行,命令执行时不查询shell函数.command命令只能够执行shell内部的命令. 语法 command(参数) 参数 指令:需要调用的指令及参数. 实例 使用 ...

  3. id---显示用户ID

    d命令   id命令可以显示真实有效的用户ID(UID)和组ID(GID).UID 是对一个用户的单一身份标识.组ID(GID)则对应多个UID 语法 id [-gGnru][--help][--ve ...

  4. 用内置的库turtle来画一朵花,python3

    题目:用内置的库turtle来画一朵花 看了群主最后成像的图片,应该是循环了36次画方框,每次有10度的偏移. 当然不能提前看答案,自己试着写代码. 之前有用过海龟画图来画过五角星.奥运五环.围棋盘等 ...

  5. 10款jQuery/CSS3动画应用 超有用

    http://www.html5tricks.com/10-jquery-css3-animation.html

  6. 27.Node.js模块系统

    转自:http://www.runoob.com/nodejs/nodejs-module-system.html 为了让Node.js的文件可以相互调用,Node.js提供了一个简单的模块系统. 模 ...

  7. 基于jQuery的楼层案例

    ~(function() { var flag = true; //点击切换效果 $(".oDR7_asideItem:not(:first)").click(function() ...

  8. BZOJ4182: Shopping(点分治,树上背包)

    Description 马上就是小苗的生日了,为了给小苗准备礼物,小葱兴冲冲地来到了商店街.商店街有n个商店,并且它们之间的道路构成了一颗树的形状. 第i个商店只卖第i种物品,小苗对于这种物品的喜爱度 ...

  9. 14.inline与namespace使用

    #include <iostream> using namespace std; namespace all { //inline作用为默认调用 inline namespace V201 ...

  10. seaJS注意点:

    1.require 是同步往下执行,require.async 则是异步回调执行.require.async 一般用来加载可延迟异步加载的模块.