我使用反应形式,我有多个输入,我想为三个输入显示一个错误,验证是;字段为必填项,不应输入空格。
组件.html:
<form [formGroup]="contratoForm" class="campo-form">
<div class="campoFormulario">
<label class="etiquetaFormulario" for="txtCodigoContrato">Código contrato</label>
<div style="display:inline-block; position: relative;" class="control">
<input [formControl]="txtCodigoContrato" type="text" id="txtCodigoContrato" name="txtCodigoContrato" class="cajaTexto ancho80" />
<span>/</span>
</div>
<div style="display:inline-block; position: relative;" class="control">
<input [formControl]="txtCodigoContrato2" type="text" id="txtCodigoContrato2" name="txtCodigoContrato2" class="cajaTexto ancho80" />
</div>
</div>
<div class="campoFormulario">
<label class="etiquetaFormulario" for="txtContrato">Contrato</label>
<div style="display:inline-block; position: relative;" class="control">
<input [formControl]="txtContrato" type="text" id="txtContrato" name="txtContrato" class="cajaTexto ancho350" />
</div>
</div>
<div *ngIf="((!txtCodigoContrato.valid && (txtCodigoContrato.dirty || txtCodigoContrato.touched)) && (txtCodigoContrato.hasError('required') || txtCodigoContrato.hasError('hasWhites')))
||
((!txtCodigoContrato2.valid && (txtCodigoContrato2.dirty || txtCodigoContrato2.touched)) && (txtCodigoContrato2.hasError('required') || txtCodigoContrato2.hasError('hasWhites')))
||
((!txtContrato.valid && (txtContrato.dirty || txtContrato.touched)) && (txtContrato.hasError('required') || txtContrato.hasError('hasWhites')))
">
<span>check the fields in red, they are required</span>
</div>
<div class="buttons">
<button class="btn btn-primary boton" type="button" style="float:right;" (click)="onCloseLink()">
<span class="glyphicon glyphicon-off"></span> Cancel
</button>
<button class="btn btn-primary boton" type="button" style="float:right;" (click)="limpiar()">
<span class="glyphicon glyphicon-trash"></span> Clean
</button>
<button type="submit" [disabled]="!contratoForm.valid" class="btn btn-primary boton" style="float:right;" (click)="onClick()">
<span class="glyphicon glyphicon-floppy-disk"></span> Save
</button>
</div>
</form>
组件.ts:
import { Component, HostBinding, OnDestroy, OnInit, Input, Output, EventEmitter, ElementRef, NgModule, ViewChild, ChangeDetectorRef } from '@angular/core';
import { Validators, FormBuilder, FormControl, FormGroup, AbstractControl} from '@angular/forms';
import { AfterViewChecked } from '@angular/core/src/metadata/lifecycle_hooks';
@Component({
selector: 'app-formulario-contrato',
templateUrl: './contrato.component.html',
styleUrls: ['./contrato.component.css']
})
export class FormularioContratoComponent {
contratoForm: FormGroup;
constructor(private elRef: ElementRef, private formBuilder: FormBuilder, private cdRef: ChangeDetectorRef) {
this.createForm();
}
txtCodigoContrato = new FormControl('', [
Validators.required
,this.hasNotWhites
]);
txtCodigoContrato2 = new FormControl('', [
Validators.required
, this.hasNotWhites
]);
txtContrato = new FormControl('', [
Validators.required
, this.hasNotWhites
]);
createForm() {
this.contratoForm = this.formBuilder.group({
txtCodigoContrato: this.txtCodigoContrato
,txtCodigoContrato2: this.txtCodigoContrato2
,txtContrato: this.txtContrato
});
}
hasNotWhites(fieldControl: FormControl) {
if (fieldControl.value.trim() != '') {
return null
}
else {
return { hasWhites: true };
}
}
ngAfterViewChecked() {
this.cdRef.detectChanges();
}
limpiar() {
}
onClick() {
return null;
}
组件.css:
/* some stuff...*/
:host /deep/ .control input.ng-invalid.ng-touched {
border-color: #ff8080;
}
验证工作正常,也就是说,当字段为空或我输入空格时,它们会跳转。我的问题是我必须添加更多表单控件,如果它包含消息,则可能难以辨认:
<div *ngIf="((!txtCodigoContrato.valid && (txtCodigoContrato.dirty ||
txtCodigoContrato.touched)) && (txtCodigoContrato.hasError('required') ||
txtCodigoContrato.hasError('hasWhites')))
||
((!txtCodigoContrato2.valid && (txtCodigoContrato2.dirty ||
txtCodigoContrato2.touched)) && (txtCodigoContrato2.hasError('required') ||
txtCodigoContrato2.hasError('hasWhites')))
||
((!txtContrato.valid && (txtContrato.dirty || txtContrato.touched))
&& (txtContrato.hasError('required') || txtContrato.hasError('hasWhites')))
">
有没有办法通过打字稿来控制这些值,也就是说,每次控件中的值发生变化时,它都由 typeScript 中返回 true 或 false 的函数控制,并且如果只在我的 div 中询问该函数的值?