如何在 angular 2 中的 material select 框中显示 md-error?
How to show md-error in material select box in angular 2?
我在我的 angular 2 项目中使用响应式表单进行验证。我想突出显示按下 'submit' 时无效的字段。我已经通过使用 md-Error 在输入标签中实现了这一点,但我无法在 md-Select 中做到这一点。有人可以帮忙吗?
截图:http://i.imgur.com/uOQbwaZ.png
这是我正在使用的 md-select 的一个例子:
<md-select placeholder="Listing Type" formControlName='listingType' required >
<md-option *ngFor="let type of listType" [value]="type">
{{ type }}
</md-option>
</md-select>
这是我正在使用的 md 输入:
<md-input-container class="more-width">
<input mdInput formControlName='price' required placeholder="Price">
<md-error>Please Enter Price</md-error>
</md-input-container>
这是我正在申请的验证
this.listingForm = this.fb.group({
propertyType: ['', Validators.required]
})
您可以在框下方显示红线,但目前没有错误消息。我认为这是一个很快就会出现的功能。 Issue on Github
您可以在 angular material 文档 here.
中找到一个验证电子邮件地址的好例子
此错误现已解决。您只需要将 mat-error
放在 mat-select
外面和 mat-form-field
里面。这是一个例子
<mat-form-field appearance="fill">
<mat-label>Favorite animal</mat-label>
<mat-select [formControl]="animalControl" required>
<mat-option>--</mat-option>
<mat-option *ngFor="let animal of animals" [value]="animal">
{{animal.name}}
</mat-option>
</mat-select>
<mat-error *ngIf="animalControl.hasError('required')">Please choose an
animal</mat-error>
</mat-form-field>
详情可以查看以下link
Error Messages in Angular form fields
我在我的 angular 2 项目中使用响应式表单进行验证。我想突出显示按下 'submit' 时无效的字段。我已经通过使用 md-Error 在输入标签中实现了这一点,但我无法在 md-Select 中做到这一点。有人可以帮忙吗?
截图:http://i.imgur.com/uOQbwaZ.png
这是我正在使用的 md-select 的一个例子:
<md-select placeholder="Listing Type" formControlName='listingType' required >
<md-option *ngFor="let type of listType" [value]="type">
{{ type }}
</md-option>
</md-select>
这是我正在使用的 md 输入:
<md-input-container class="more-width">
<input mdInput formControlName='price' required placeholder="Price">
<md-error>Please Enter Price</md-error>
</md-input-container>
这是我正在申请的验证
this.listingForm = this.fb.group({
propertyType: ['', Validators.required]
})
您可以在框下方显示红线,但目前没有错误消息。我认为这是一个很快就会出现的功能。 Issue on Github
您可以在 angular material 文档 here.
中找到一个验证电子邮件地址的好例子此错误现已解决。您只需要将 mat-error
放在 mat-select
外面和 mat-form-field
里面。这是一个例子
<mat-form-field appearance="fill">
<mat-label>Favorite animal</mat-label>
<mat-select [formControl]="animalControl" required>
<mat-option>--</mat-option>
<mat-option *ngFor="let animal of animals" [value]="animal">
{{animal.name}}
</mat-option>
</mat-select>
<mat-error *ngIf="animalControl.hasError('required')">Please choose an
animal</mat-error>
</mat-form-field>
详情可以查看以下link Error Messages in Angular form fields