Pri*_*hra 5 css autocomplete angular-material angular angular7
我们已经使用Angular 材质的自动完成功能创建了一个组件。为了显示选项,我们遍历了一个包含 51 个对象的数组。我正在将 CSS 类应用于已选择的选项。该isAccountingTypeSelected方法确定选项是否被选中或没有。该方法被调用51*28 = 1428 次。我似乎不明白原因?它应该只被调用51 次,不是吗?
<mat-form-field class="full-width">
<input type="text" matInput #autoCompleteInput [formControl]="autocompleteForm" [matAutocomplete]="auto" placeholder="Choose Accounting Type" aria-label="Number">
<span matSuffix class="close-icon hover" *ngIf="autoCompleteInput.value" (click)="clearAll($event)"></span>
<span matSuffix class="arrow-drop-down-icon hover" (click)="openPanel()"></span>
<mat-autocomplete #auto="matAutocomplete" (optionSelected)="accountingTypeSelected($event)">
<mat-option *ngFor="let accountingType of filteredAccountingTypes | async" [value]="accountingType.code">
<span class="accounting-type-options" [class.selected]="isAccountingTypeSelected(accountingType.code)">
{{ accountingType.name + ' (' + accountingType.code + ')' }}
</span>
</mat-option>
</mat-autocomplete>
</mat-form-field>
isAccountingTypeSelected(code: string): boolean {
console.log('I was called');
if (this.selectedAccountingTypes.find((accountingType: AccountingType) => accountingType.code === code)) {
return true;
}
return false;
}
Run Code Online (Sandbox Code Playgroud)