要重构Angular mat-selection-list项的删除功能,可以按照以下步骤进行操作。
ng add @angular/material
import { MatSelectionList, MatListOption } from '@angular/material/list';
listItems: string[] = ['Item 1', 'Item 2', 'Item 3'];
{{ item }}
onSelectionChange(options: MatListOption[]) {
const deletedOptions = options.filter(option => !option.selected);
this.listItems = deletedOptions.map(option => option.value);
}
deleteSelectedItems() {
const selectedOptions = this.list.selectedOptions.selected;
selectedOptions.forEach(option => {
const index = this.listItems.indexOf(option.value);
if (index >= 0) {
this.listItems.splice(index, 1);
}
});
}
通过以上步骤,你就可以实现使用Angular Material的mat-selection-list来删除选中的项了。