简体   繁体   中英

Prevent Mat-Option from closing the dropdown

I want to know if it is possible to prevent the closing of the dropdown if one mat-option is selected? I am using stopPropagation and preventDefault on the click event, but it doesn't work. Please have a look at the following code.

  • aktual behaviour: clicking on 'Toyota' closes the dropdown
  • expected behaviour: clicking on 'Toyota' shouldn't close the dropdown
<mat-form-field appearance="fill">
  <mat-label>Cars</mat-label>
  <mat-select [formControl]="carControl" multiple>
    <mat-select-trigger>
      {{carControl.value}}
    </mat-select-trigger>
    <mat-option value="BMW">BMW</mat-option>
    <mat-option value="VW">VW</mat-option>
    <mat-option value="Toyota" (click)="$event.preventDefault(); $event.stopPropagation()">Toyota</mat-option>
  </mat-select>
</mat-form-field>

Any help is welcome.

I found a workaround. Disabling the option which shouldn't close the dropdown and changing the styling of the disabled option, so it matches the other active options.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM