【发布时间】:2020-06-24 04:06:55
【问题描述】:
component.html
<div class="form-group">
<label>Enter mobile</label>
<input type="text" class="form-control" formControlName="mobile" ><br>
</div>
<div *ngIf="userGroup.controls.mobile.invalid && (userGroup.controls.mobile.dirty || userGroup.controls.mobile.touched)">
<div *ngIf="userGroup.controls.mobile.errors.required">
Mobile number cannot be blank
</div>
<div *ngIf="userGroup.controls.mobile.errors.pattern">
Mobile number should be 10 digits only
</div>
</div>
组件.ts
userGroup:FormGroup;
ngOnInit() { this.userGroup = this.fb.group({
mobile:['',Validators.required,Validators.pattern(/^[0-9]{10}$/)]
});
}
对于空白,它工作得很好,但对于模式,它没有显示任何错误
【问题讨论】:
-
我认为您的控制台会抛出以下错误:预期的验证器会返回承诺或可观察到的角度。所以请使用以下链接解决问题。 stackoverflow.com/questions/50548062/…
-
我认为@Jinu 的回答可以解决您的问题。有关更多信息,请查看我提供的链接。
标签: javascript node.js angular angular-reactive-forms