[Angular2 Form] Nested formGroup, and usage of formGroupName
and of Form nested Usage Angular2
2023-09-14 08:59:19 时间
We can nest formGorup:
this.reactiveForm = fb.group({ username: [ '', [ Validators.required, Validators.minLength(3) ] ], 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:
相关文章
- SP Module 1 - Phonetics and Representations of Speech
- 1267-Illegal mix of collations (utf8mb4_general_ci,IMPLICIT) and (utf8mb4_0900_ai_ci,IMPLIC for o...
- A. Vasya and Book ( Codeforces Educational Codeforces Round 55 )
- ORA-01875: time zone minute must be between -59 and 59 ORACLE 报错 故障修复 远程处理
- ORA-23363: mismatch of mview base table “string” at master and mview site ORACLE 报错 故障修复 远程处理
- ORA-30493: The percentile value should be a number between 0 and 1. ORACLE 报错 故障修复 远程处理
- ORA-55617: Flashback Archive “string” runs out of space and tracking on “string” is suspended ORACLE 报错 故障修复 远程处理
- MySQL Error number: MY-010476; Symbol: ER_NDB_NODE_ID_AND_MANAGEMENT_SERVER_INFO; SQLSTATE: HY000 报错 故障修复 远程处理
- ORA-13621: The task_or object string is marked as a template and cannot perform the requested operation. ORACLE 报错 故障修复 远程处理
- ORA-13903: Invalid combination of string threshold value and operator. ORACLE 报错 故障修复 远程处理
- what is the difference of select single and select up to one row in abap详解编程语言
- Linux: Explore a Type of Free and Open Source Operating System(linuxtype)
- The Power of Man and Linux: Redefining Computing(man.linux)
- Efficient Data Cleaning with MongoDB: Tips and Tricks(mongodb清理数据)
- Exploring the Benefits of Oracle: Enhancing Performance and Streamlining Operations(oracle的好处)
- Exploring the Power of Linux 2G: A Guide to Maximizing Performance and Efficiency(linux2g)
- Exploring the Advantages and Disadvantages of Using .myd Files in MySQL Databases(mysql.myd)
- Effective Linux DDoS Mitigation Techniques: How to Keep Your System Safe and Secure?(linux防御ddos)
- Exploring the Power and Versatility of SAR on the Linux Platform(sarlinux)
- The Power and Efficiency of a Linux Server: Everything You Need to Know(linuxsever)
- Exploring the Power of MySQL and C for Advanced Database Management(mysqlcpp)
- Mastering Linux Configuration with C Programming and INI Files(linuxcini)
- Exploring the Benefits and Features of MSSQL Server Software(mssql服务器软件)
- Exploring the Versatile World of Linux PDA: Features Applications and Advantages(linuxpda)
- Maximizing Productivity with Linux and Maven: A Comprehensive Guide(linuxmaven)
- MySQL中如何正确使用AND运算符(mysql中and怎么用)