Angular mat-selection-list, How to make single checkbox select similar to radio button?

后端 未结 14 1764
北恋
北恋 2021-02-18 21:22

How to make make single checkbox select from mat-selection-list. Similar to radio button which accepts one value from group of values.

14条回答
  •  余生分开走
    2021-02-18 21:34

    Components >= 9.1.0

    Selection list now supports a single selection mode directly. Enable it with the multiple input set to false.

    ...
    

    Components < 9.1.0

    You have to set SelectionModel for selectedOptions property of component reference on init.

    @ViewChild(MatSelectionList, {static: true})
    private selectionList: MatSelectionList;
    
    ngOnInit() {
        this.selectionList.selectedOptions = new SelectionModel(false);
    }
    

    MatSelectionList api

    This way you can define initially selected values too.

    export declare class SelectionModel {
        constructor(_multiple?: boolean, initiallySelectedValues?: T[], _emitChanges?: boolean);
    }
    

    GitHub api

提交回复
热议问题