在 Angular Material 组件列表中,发现在早期版本中初始化时的选择不会被报告。这可能会给开发人员带来困扰,然而,这个问题已经在后来的版本中得到了修复。
解决方法可以是升级版本,或者在代码中加入“ngAfterViewInit”来设置初始选择。下面是代码示例:
import { Component, ViewChild, AfterViewInit } from '@angular/core';
import { MatSelectionList } from '@angular/material/list';
@Component({
selector: 'app-your-component',
templateUrl: './your-component.component.html',
styleUrls: ['./your-component.component.scss']
})
export class YourComponentComponent implements AfterViewInit {
@ViewChild(MatSelectionList) selectionList: MatSelectionList;
ngAfterViewInit(): void {
this.selectionList.selectedOptions.selected.forEach(item => {
console.log(item.value); // 初始选择
});
}
}