如何让我的自定义mat-error
立即触发?我知道 Angular 文档中有一个示例,但无法使其正常工作。我的验证不是为了empty
也不是为了某个type
,例如email
。
当我在下拉字段中进行搜索但未找到任何记录时,我希望显示我的错误。<div>
项目下方的正常立即触发*ngIf
,但不是mat-error
。我也尝试使用 更新 mat-error .innerHTML
,但该字段不存在。我确信这是因为 Angular 还没有创建它。
这是我的 HTML:
<mat-form-field>
<input type="text" placeholder="Customer Search" id="CustomerId" name="CustomerId" aria-label="Number" matInput [formControl]="myCustomerSearchControl"
[matAutocomplete]="auto" (keyup)="onCustomerSearch($event)"
required [errorStateMatcher]="matcher">
<mat-autocomplete autoActiveFirstOption #auto="matAutocomplete" (optionSelected)="setLoadId($event)">
<mat-option *ngFor="let customer of customerArray" [value]="customer.Display">
{{ customer.Display }}
</mat-option>
</mat-autocomplete>
<mat-error id="customer-error" *ngIf="noCustomersFound.value">
{{noCustomersFound.message}}
</mat-error>
</mat-form-field>
<div>
<span class="errorMessage" *ngIf="noCustomersFound.value">
{{noCustomersFound.message}}
</span>
</div>
这是我在 .ts 文件中的方法以及我尝试过的一些东西:
onCustomerSearch(search) {
let searchObject = new SearchObject();
searchObject.Entity = 'Customer';
searchObject.SearchString = search.key;
searchObject.SearchClauses = [{Column: 'sCustomerName'}];
searchObject.DisplayColumn = 'sCustomerName';
this.loadDataService.searchByEntity(searchObject)
.subscribe(data => {
if (data.length < 1) {
this.noCustomersFound.value = true;
this.noCustomersFound.message = 'No match found.'
document.getElementById('customer-error').innerHTML = 'No match found.';
} else {
this.noCustomersFound.value = false;
this.noCustomersFound.message = '';
}
this.customerArray = data;
});
}