是否有任何選項可以使驗證器的最小和最大數量,即0到200之間的數字?驗證器最小和最大數字角度4
對話框的結果-component.ts
import { Component, OnInit } from '@angular/core';
import { MdDialog, MdDialogRef } from '@angular/material';
import { FormGroup,FormControl,Validators,FormBuilder, } from '@angular/forms';
@Component({
selector: 'app-dialog-result',
templateUrl: './dialog-result.component.html',
})
export class DialogResultComponent {
form: FormGroup;
name = new FormControl('',Validators.required);
width = new FormControl('',Validators.required);
height = new FormControl('',Validators.required);
constructor(public dialogRef: MdDialogRef<DialogResultComponent>,private fb: FormBuilder) {
}
ngOnInit() {
this.form = this.fb.group({
'name': this.name,
'width': this.width,
'height': this.height,
});
}
saveData(){
this.dialogRef.close({name:this.name,width:this.width,height:this.height});
}
}
對話框的result.component.html
<form [formGroup]="form">
<h3>MineSweeperwix</h3>
<div class="mdl-dialog__content">
<p><mdl-textfield type="text" label="name" formControlName="name" floating-label autofocus></mdl-textfield></p>
<mdl-textfield type="number" formControlName="width" min="0" max="350" label="width" floating-label autofocus></mdl-textfield>
<mdl-textfield type="number" formControlName="height" label="height" min="0" max="350" floating-label autofocus error-msg="'Please provide a correct email!'"></mdl-textfield>
</div>
<div class="mdl-dialog__actions">
<button mdl-button (click)="saveData()" mdl-button-type="raised" mdl-colored="primary" mdl-ripple [disabled]="!form.valid">Save</button>
<button mdl-button (click)="dialogRef.close(dd)" mdl-button-type="raised" mdl-ripple>Cancel</button>
</div>
</form>
最小和最大到的MDL-文本字段
min="0" max="200" floating-label autofocus
限制用戶在範圍內寫入數字,但它讓按下保存按鈕,這不是我想要的這樣做。我希望用戶可以按下保存按鈕,只要所有的表單都有效。
什麼,我試圖做的是
dialog.result.component.ts
import { Component, OnInit } from '@angular/core';
import { MdDialog, MdDialogRef } from '@angular/material';
import { FormGroup,FormControl,Validators,FormBuilder,ValidatorFn,AbstractControl } from '@angular/forms';
import { NumberValidators } from '../validators/NumberValidators.module';
@Component({
selector: 'app-dialog-result',
templateUrl: './dialog-result.component.html',
})
export class DialogResultComponent {
form: FormGroup;
name = new FormControl('',Validators.required);
width = new FormControl('',[Validators.required,NumberValidators.range(3,300)]);
height = new FormControl('',[Validators.required,NumberValidators.range(3,300)]);
constructor(public dialogRef: MdDialogRef<DialogResultComponent>,private fb: FormBuilder) {
}
ngOnInit() {
this.form = this.fb.group({
'name' :this.name,
'width': this.width,
'height': this.height
});
}
saveData(){
console.log(this.name.value);
this.dialogRef.close({name:this.name.value,width:this.width.value,height:this.height.value});
}
}
NumberValidators.module.ts
import { AbstractControl, ValidatorFn } from '@angular/forms';
export class NumberValidators {
static range(min: number, max: number): ValidatorFn {
console.log(min+max);
return (c: AbstractControl): { [key: string]: boolean } | null => {
if (c.value && (isNaN(c.value) || c.value < min || c.value > max)) {
return { 'range': true };
}
return null;
};
}
}
,但它不是工作正常任何人有任何建議?
黛博拉,我帶着這個驗證器並嘗試使用它。它顯示我一個錯誤。我用它像那樣width = new FormControl('',Validators.required,NumberValidators.range(3,300)); –
它顯示「類型ValidatorFn的參數」不能分配給類型爲'asynceValidatorFn ..... –
的參數第二個參數用於正常驗證器,第三個參數用於異步驗證器。這不是一個異步驗證器。您需要通過將第二個參數作爲數組傳遞來將其添加爲第二個參數的一部分。像這樣:[validators.required,NumberValidators.range(3,300)] – DeborahK