我有一个输入,类型是数字。我希望设置min和max,如果输入超出此范围(< min或>max),将显示错误。
我的问题是,如果输入不是空的,而是无效的,错误就会消失(例如: min =10,max =20,but =2000,仍然没有显示错误)。
我在这个站点上搜索,并且有一篇关于验证的文章,但是解决方案是使用和< md-error>。我不想使用和。
参考资料:这里
有办法解决我的问题吗?
我的add-英雄.组件.hero.component.html:
<div class="publishedYear">
<label>Publish Year: </label>
<input type="number" id="PublishedYear" name="PublishedYear" required min="10" max="20"
[(ngModel)]="hero.PublishedYear" #publishedYear="ngModel">
<p class="alert alert-danger invalid" *ngIf="publishedYear.errors">Invalid Published Year</p>
</div>我的Hero.ts
export class Hero {
Id: number;
Name: string;
PublishedYear: number;
Complete?: boolean;
}发布于 2018-10-24 10:42:42
谢谢你以上所有的回答和你的努力。经过几个小时的研究,我终于得到了一个答案:页面
如果你有像我这样的问题,这就是答案。
我的.html文件:
<form [formGroup]="myForm">
<label for="publishedYear">Publish Year: </label>
<input type="number" id="PublishedYear" formControlName="publishedYear">
<div *ngIf="f.publishedYear.errors">
Invalid Published Year
</div>
我的.ts文件:
import { Component, OnInit, Input } from '@angular/core';
import { Hero } from '../Hero';
import { HeroService } from '../hero.service';
import { Router } from '@angular/router';
import { FormBuilder, FormGroup, Validators, ReactiveFormsModule } from
'@angular/forms';
@Component({
selector: 'app-hero-add',
templateUrl: './hero-add.component.html',
styleUrls: ['./hero-add.component.css']
})
export class HeroAddComponent implements OnInit {
hero: Hero = new Hero();
myForm = new FormGroup({}) // Instantiating our form
get f() { return this.myForm.controls; }
constructor(private heroService: HeroService, private router: Router, private
formBuilder: FormBuilder) {
this.myForm = formBuilder.group({
publishedYear: ['', [Validators.min(1990), Validators.max(2018)]]
});
}
ngOnInit() {
}
}发布于 2018-10-24 09:21:43
对于您的解决方案,在使用输入时,type="number"设置min和max只允许用户使用滚动器增加或减少数量。但是当用户直接在文本中输入数字时,它不会显示任何错误
要做到这一点,有两个解决方案
1)使用角表单验证将表单控件添加到输入中,将有几个在线示例
2)在文本框的“打开”或单击“按钮”上调用一个函数,以验证用户输入的数字与ts文件中的表达式匹配。
使用表单验证您需要做的
myForm = new FormGroup({}) // Instantiating our form
constructor(private fb: FormBuilder){ // Injecting the ReactiveForms FormBuilder.
this.myForm = fb.group({
// Adding the "myNum" input to our FormGroup along with its min-max Validators.
'myNum': ['', [Validators.min(5), Validators.max(10)]]
})
}在HTML中
<form [formGroup]="myForm"> <!-- Binding myForm to the form in the template -->
<label for="myNum">Some Val</label>
<!-- formControlName binds our myNum field declared in the ts code. -->
<input type='number' id="myNum" formControlName="myNum">
<div *ngIf="myForm.controls['myNum'].hasError('max')">
Minimum required number is 15.
</div>
</form>发布于 2018-10-24 09:23:15
import { Component } from '@angular/core';
import { Directive, Input, forwardRef } from "@angular/core";
import {
Validator, AbstractControl, NG_VALIDATORS, Validators, ValidatorFn
} from "@angular/forms";
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
hero: any = {};
}
@Directive({
selector: "[min][formControlName],[min][formControl],[min][ngModel]",
providers: [
{ provide: NG_VALIDATORS,
useExisting: forwardRef(() => MinDirective),
multi: true }
]
})
export class MinDirective implements Validator {
private _validator: ValidatorFn;
@Input() public set min(value: string) {
this._validator = Validators.min(parseInt(value, 10));
}
public validate(control: AbstractControl): { [key: string]: any } {
return this._validator(control);
}
}
@Directive({
selector: "[max][formControlName],[max][formControl],[max][ngModel]",
providers: [
{ provide: NG_VALIDATORS,
useExisting: forwardRef(() => MaxDirective),
multi: true }
]
})
export class MaxDirective implements Validator {
private _validator: ValidatorFn;
@Input() public set max(value: string) {
this._validator = Validators.max(parseInt(value, 10));
}
public validate(control: AbstractControl): { [key: string]: any } {
return this._validator(control);
}
}
<input type="number" [(ngModel)]="hero.count" name="count" #count="ngModel" required min="1" max="100">
<p *ngIf="count.invalid">Invalid Published Year</p>将两个指令添加到声明中。这应该适用于模板驱动的表单。
https://stackoverflow.com/questions/52964626
复制相似问题