私は複数の入力を持っています。フィールドは必須で、空白は入力しないでください。Angular5を使用してフォームコントロールの変更をチェックする方法
component.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>
component.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;
}
component.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を返すtypeScriptの関数によって制御され、div内のその関数の値を尋ねるだけの場合は変更されますか?
に自分のコントロール名を追加し、これは動作しますが、唯一の私は変更を行う際にジャンプSTACKBLITZ
を作成しました入力。カーソルをある入力から別の入力に移動するときにジャンプする必要があります。 – ararb78