【发布时间】:2019-10-15 14:03:29
【问题描述】:
通过阅读多个 Angular 资源(见下文),在模板中调用方法可能会导致性能问题。例如
<div>{{getDisplayName()}}</div>
但是,很多 Angular Material 库示例调用模板中的方法。
树
https://material.angular.io/components/tree/examples
<mat-icon class="mat-icon-rtl-mirror">
{{treeControl.isExpanded(node) ? 'expand_more' : 'chevron_right'}}
</mat-icon>
带选择的表格
https://material.angular.io/components/table/examples
<td mat-cell *matCellDef="let row">
<mat-checkbox (click)="$event.stopPropagation()"
(change)="$event ? selection.toggle(row) : null"
[checked]="selection.isSelected(row)"
[aria-label]="checkboxLabel(row)">
</mat-checkbox>
</td>
这些方法确实会从 Set 中返回一个值。例如。 https://github.com/angular/components/blob/4d4ee182de3f306c5fcf49853ef6ac71c4000eea/src/cdk/collections/selection.ts
isSelected(value: T): boolean {
return this._selection.has(value);
}
这些方法会像 *normal 方法一样导致性能问题吗?还是我缺少什么?
资源
Does calling functions in templates cause performance issues in Angular2+?
【问题讨论】:
-
第一个问题写得很好。 (答案来了)
-
@cmprogram 谢谢!
标签: javascript angular angular2-template