gpt4 book ai didi

Angular 响应式(Reactive)验证

转载 作者:太空狗 更新时间:2023-10-29 18:35:17 24 4
gpt4 key购买 nike

我是 Angular 的新手,我想知道 angular 6.0 中的响应式(Reactive)验证是如何工作的,我正在尝试进行过去 2 周的验证,有人可以帮我进行验证吗?并教我怎么做。

非常感谢您的帮助,谢谢 :)

import { Component } from '@angular/core';
import { ReactiveFormsModule, FormsModule, FormGroup, FormBuilder, FormControl, Validators } from '@angular/forms';

@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'AngularProject';

constructor(private fb:FormBuilder)
{}
profileForm : FormGroup;
submitted = false;
ngOnInit(): void {
this.profileForm= this.fb.group({
firstName:['', [Validators.required]],
EmailMe:['',[Validators.required]]
});
}

onSubmit():void
{
this.submitted = true;
if (this.profileForm.invalid) {
return;
}
console.log(this.profileForm.value);
alert('SUCCESS!! :-)\n\n' + JSON.stringify(this.profileForm.value))
}
get f() {
return this.profileForm.controls;
}}

HTML 部分如下。

<form class="form-horizontal" [formGroup]="profileForm" (ngSubmit)="onSubmit()">
<div class="panel panel-primary">

<div class="panel-heading">
<h3 class="panel-title">C3</h3>
</div>


<div class="panel-body">
<div class="form-group">
<label class="col-sm-2 control-label" for="fullName">Full Name</label>
<div class="col-sm-8">
<input id="fullName" type="text" class="form-control" formControlName="firstName"
[ngClass]="{ 'is-invalid': submitted && f.profileForm.errors }" >

<div *ngIf="f.firstName.errors.required">First Name is required {{f.profileForm.errors}}</div>
<div *ngIf=></div>
</div>
</div>

<div class="form-group">
<label class="col-sm-2 control-label" for="email">Email</label>
<div class="col-sm-8">
<input id="email" type="text" class="form-control" formControlName="EmailMe">
</div>
</div>

</div>
<div class="panel-footer">
<button class="btn btn-primary" type="submit" [disabled]="!profileForm.valid">Save</button>
</div>
</div>
</form>
<!-- -->

<router-outlet></router-outlet>

最佳答案

您的验证检查似乎有误。在这里,试一试:

<form 
class="form-horizontal"
[formGroup]="profileForm"
(ngSubmit)="onSubmit()">

<div class="panel panel-primary">

<div class="panel-heading">
<h3 class="panel-title">C3</h3>
</div>


<div class="panel-body">

<div class="form-group">
<label
class="col-sm-2 control-label"
for="fullName">
Full Name
</label>

<div class="col-sm-8">
<input
id="fullName"
type="text"
class="form-control"
formControlName="firstName">
<div
*ngIf="profileForm.controls['firstName'].touched && profileForm.controls['firstName'].errors.required"
>
First Name is required
</div>
</div>
</div>

...

</div>

...

</div>

</form>

Angular 还会自动将 ng-invalid 和 ng-touched 等类应用于无效和已触摸的表单控件。所以你可以利用它来简化你的 CSS:

input.ng-touched.ng-invalid {
border: 1px solid red;
}

Here's a Working Sample StackBlitz for your ref.

关于 Angular 响应式(Reactive)验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57293147/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com