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. 用Vue+axios写一个实时搜索

    刚刚在学vue,试着写了一个实时搜索文件. 思路:1.input 通过v-model绑定.2.通过watch检测输入结果变化.3根据结果变化从api调用不同的数据. 代码如下: <!DOCTYP ...

  2. 【Python学习】爬虫报错处理bs4.FeatureNotFound

    [BUG回顾] 在学习Python爬虫时,运Pycharm中的文件出现了这样的报错: bs4.FeatureNotFound: Couldn’t find a tree builder with th ...

  3. 编程算法 - 水洼的数量 代码(C)

    水洼的数量 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 有一个大小为N*M的园子, 雨后起了积水. 八连通的积水被觉得是连接在一起的. 请求 ...

  4. 一些VPS

    https://www.perfectip.net                                        5美元/1C/4G/20G/10Thttps://www.hetzne ...

  5. JSP中使用EL表达式

    EL表达式 :EL 全名为Expression Language,就是为了替代<%= %>脚本表达式. EL主要作用: 获取数据: EL表达式主要用于替换JSP页面中的脚本表达式,以从各种 ...

  6. HDU 2281 Square Number Pell方程

    http://acm.hdu.edu.cn/showproblem.php?pid=2281 又是一道Pell方程 化简构造以后的Pell方程为 求出其前15个解,但这些解不一定满足等式,判断后只有5 ...

  7. 洛谷 【P1252】马拉松接力赛

    洛谷 [P1252]马拉松接力赛 题目描述 某城市冬季举办环城25km马拉松接力赛,每个代表队有5人参加比赛,比赛要求每个的每名参赛选手只能跑一次,一次至少跑1km.最多只能跑10km,而且每个选手所 ...

  8. 【2017"百度之星"程序设计大赛 - 初赛(B)】度度熊的交易计划

    [链接]点击打开链接 [题意] 在这里写题意 [题解] 先设一个超级源点,向每个片区都建一条边,容量为b,费用为-a; 然后从每个片区再连一条边,指向一个超级汇点. 容量为d,费用为c; 然后从起点到 ...

  9. JSP页面开发规范案例

    添加 <%@ page language="java" contentType="text/html; charset=utf-8" pageEncodi ...

  10. C#中数组与ArrayList的简单使用

    1. 多维数组 2. 锯齿数组 3. 数组的常用操作 4. ArrayList 1. 多维数组 多维数组:行数和列数在定义时已确定 string[,] arr = new string[2, 3]; ...