首页 > 解决方案 > 如果 MatSelctionList,如何获取屏幕上可见的项目数量?

问题描述

我想在代码中知道屏幕上的材料选择列表中显示了多少项目。

我尝试使用:

@ViewChild(MatSelectionList) matSelectionList: MatSelectionList;

但是我没有看到任何带有该信息的变量或函数。有没有人有类似的问题?

这样做的目的很简单:我的应用程序中有文件资源管理器,当我升级时,我需要知道有多少元素。

标签: angulartypescriptlistangular-material

解决方案


这里是<mat-selection-list> https://run.stackblitz.com/api/angular/v1?file=src%2Fapp%2Flist-selection-example.ts的官方文档

<mat-selection-list #shoes>
  <mat-list-option *ngFor="let shoe of typesOfShoes">
    {{shoe}}
  </mat-list-option>
</mat-selection-list>

<p>
  Options selected: {{shoes.selectedOptions.selected.length}}
</p>

推荐阅读