【发布时间】:2020-01-11 09:14:48
【问题描述】:
我们使用 Angular 材料 的 autocomplete 创建了一个组件。为了显示选项,我们遍历了一个 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;
}
【问题讨论】:
标签: css angular autocomplete angular-material angular7