【发布时间】:2018-06-04 05:19:18
【问题描述】:
我正在尝试对 angular 6 mat-dialog 进行验证,以便在单击 mat-dialog 选项时收到错误消息。如果该项目没有在 mat-dialog 选项验证生成。如何做到这一点?我尝试了很多,但没有得到输出。帮我解决这个问题。
下面是我的代码
getError() {
this.stringData='Please provide a valid carrier id';
return this.stringData;}filter(val: any): any[] {
console.log(this.editFormData.controls['carrierID'].value);
let valid;
this.prepaidCarrierTripId.map(x => {
if (x.trpCarrierId.startsWith(this.editFormData.controls['carrierID'].value)) {
valid = true;
}
})
if (!valid) {
this.getError();
}
else
this.error = null;
return this.prepaidCarrierTripId.filter(option =>
option.trpCarrierId.toLowerCase().includes(val.toLowerCase()));}
我的html
<input matInput type="text" name="carrierID" (click)="load()" formControlName="carrierID"
placeholder="{{ 'Carrier ID'}}" maxlength="6" [matAutocomplete]="auto">
<mat-error class="error-msg" [value]="getError()">{{getError()}}</mat-error>
<mat-error class="error-msg">CarrierId is required</mat-error>
<mat-autocomplete autoActiveFirstOption #auto="matAutocomplete" (optionSelected)="selected($event)">
<mat-option *ngFor="let option of filteredOptions | async" [value]="option.trpCarrierId">{{option.trpCarrierId}}</mat-option>
</mat-autocomplete>
</mat-form-field>
【问题讨论】:
-
您是否尝试过将表单验证放入您的打字稿表单对象中,例如
form:{someControlName : newFormControl('', Valdators.minLength(6)i)} -
不,我尝试了该功能,当验证出错时,控制台显示错误消息,但 mat 对话框不显示。我认为我的 mat-error 不好。