angular - 在 Angular 中选择选项视图滞后
问题描述
我正在尝试制作一个带有国旗图像和国家名称的国家选择下拉菜单。
<mat-select placeholder="Country" ngModel name="nationality [(ngModel)]="nationality">
<mat-option *ngFor="let country of Countries" [value]="country.code" >
<div>
<img [src]="country.flag" [alt]="country.name">
<p>{{ country.name }}</p>
</div>
</mat-option>
</mat-select>
现在,如果我列出世界上所有的国家,用户界面就会变得太迟钝。这绝对是由于 200 多个标志(绝对是 svg 文件)。我该如何解决这个滞后?
是否有可能仅显示可见选项的标志?
解决方案
尝试使用 onPush ChangeDetectionStrategy,这将通过确保 Angular 的更改检测仅在触发指定操作时发生,从而提高您的应用程序性能。
import { Component, Input, ChangeDetectionStrategy } from '@angular/core';
@Component({
selector: 'app-country',
changeDetection: ChangeDetectionStrategy.OnPush,
templateUrl: 'country.html'
});
export class CountryComponent {}
阅读更多:https ://blog.angular-university.io/how-does-angular-2-change-detection-really-work/
推荐阅读
- java - Tomcat - JRE_HOME 环境未正确定义 - Java 11
- php - Composer 错误:您的要求无法解析为一组可安装的软件包
- flutter - 列表
到地图 在 Dart 到 Firestore - python - 将 YUV_NV12 转换为 YUV444
- http-live-streaming - Exoplayer 播放时边缘粗糙
- vue.js - 使用 Vue CLI V4 调试 Vue 3
- css - WYSIWYG ACF 字段似乎采用了来自周围 div 的 css 样式
- azure - 在 Azure 存储数据湖中保存 JSON 文件以 NUL (Ascii 0) 字符和空格结束
- javascript - 如何以角度为 setTimeout 函数和 activeElement 编写单元测试?
- vue.js - 带有 Jitsi 或 Zoom 的 Vue3