I really want the mat-select basic functionality but with checkboxes. If I turn on [multiple] I get the checkboxes but I also get multi-select functionality, I need single select and checkbox.
MatSelectTrigger. Allows the user to customize the trigger that is displayed when the select has a value.
Create Select using <mat-select> Angular Material Select is created using <mat-select> which is a form control for selecting a value from a set of options. To add elements to select option, we need to use <mat-option> element. To bind value with <mat-option> , we need to use value property of it.
The ng-checked Directive in AngularJS is used to read the checked or unchecked state of the checkbox or radio button to true or false. If the expression inside the ng-checked attribute returns true then the checkbox/radio button will be checked otherwise it will be unchecked.
you can achieve your requirements like this,
here's is example,
in app.component.html
<mat-form-field>
<mat-select placeholder="Toppings">
<mat-option *ngFor="let topping of toppingList; let i =index" [value]="topping">
<mat-checkbox [checked]="selected === i" (change)="onChange(topping);selected = i"> {{topping}}</mat-checkbox>
</mat-option>
</mat-select>
</mat-form-field>
and in app.component.ts
export class AppComponent {
toppingList: string[] = ['Extra cheese', 'Mushroom', 'Onion', 'Pepperoni', 'Sausage'];
selected = -1;
/*checkbox change event*/
onChange(event) {
console.log(event)
}
}
here's is Stackblitz demo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With