I have a multiple mat-select
and would like to know if the mat-option
that's been clicked has been selected or deselected. The $event.target
object passed when the (click)
is fired has no selected
attribute I could use.
<mat-form-field>
<mat-select [formControl]="control" multiple>
<mat-option
*ngFor="let option of options"
[value]="option"
(click)="foo($event)"
>
{{ option }}
</mat-option>
</mat-select>
</mat-form-field>
public foo(event) {
const hasBeenChecked = ???? // How do I know if my clicked option has been checked or unchecked?
}
Thanks in advance
log the FormControl you use: [formControl]="control" , you will see that it holds the last selected option. If you have multiple mat-selects, and would like to have them in control, my suggestion is to wrap them in a FormGroup and then use FormControl which belongs to that FormGroup for each of the selects.
MatSelectTrigger. Allows the user to customize the trigger that is displayed when the select has a value.
Create Select using <mat-select> 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 <mat-select> has also value property to bind selected value to keep it selected. We need to use <mat-select> inside <mat-form-field> element.
You can get the selected state of the clicked option by reading it off of the MatOption object as follows:
<mat-option #matOption (click)="foo(matOption.selected)"></mat-option>
StackBlitz Example
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