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. js 判断是不是空、值是否存在

    判断数组是否存在某个值: Array.indexOf(val) > -1 //存在 (缺陷:一是不够语义化,它的含义是找到参数值的第一个出现位置,所以要去比较是否不等于-1,表达起来不够直观.二 ...

  2. java初探秘之推断输入的一串字符是否全为小写字母

    import java.io.IOException; import java.util.*; public class Two { public static void main(String[] ...

  3. Android学习笔记进阶14之像素操作

    在我们玩的游戏中我们会经常见到一些图像的特效,比如半透明等效果.要实现这种半透明效果其实并不难,需要我们懂得图像像素的操作. 不要怕,其实在Android中Bitmap为我们提供了操作像素的基本方法. ...

  4. js上传文件(图片)的格式和大小限制

    如果你想快速解决这个问题,看本文就够了.查了好多资料,终于解决了,太耗时间了,本文留给给后来者,希望你们工作的更轻松 本文保存为.html文件用浏览器打开即可测试功能 <form id=&quo ...

  5. BZOJ5394: [Ynoi2016]炸脖龙(欧拉广义降幂)

    就是让你求这个: 传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=5394 解题思路: NOIP2018后第一道题,感觉非常像那个上帝与集合的 ...

  6. Linux 进程通信之管道

    管道是单向的.先进先出的,它把一个进程的输出和还有一个进程的输入连接在一起.一个进程(写进程)在管道的尾部写入数据,还有一个进程(读进程)从管道的头部读出数据.数据被一个进程读出后,将被从管道中删除, ...

  7. C语言速度优化之指针赋值与if推断

    近期在写的一个项目须要优化处理速度,我写了一下程序来測试指针赋值与指针推断的速度比較.结果让我大吃一惊. #include <stdio.h> #include <stdlib.h& ...

  8. JdbcTemplate简单介绍

    转自:http://www.php.cn/java-article-368819.html 一.关于JdbcTemplate JdbcTemplate是最基本的Spring JDBC模板,这个模板支持 ...

  9. Redis笔记教程

    一.redis简介 1.1.1.什么是redis? REmote DIctionary Server(Redis) 是一个由Salvatore Sanfilippo写的key-value存储系统. 读 ...

  10. Python画图参数设置

    https://blog.csdn.net/qiu931110/article/details/68130199