简体   繁体   中英

How to get drop down text in mat-select using angular 6

Here is is my code HTML:

<mat-list-item class="primary-imenu-item" role="listitem">
    <mat-select class="form-control" multiple formControlName="statusCode" (selectionChange)="getSelectedOptionText($event)">
        <mat-option *ngFor="let list of statusList" [value]="list.id" checkboxPosition="before">
            {{list.code}}
        </mat-option>
    </mat-select>
</mat-list-item>

TS Code:

getSelectedOptionText(event: MatSelectChange) {
    let selectedData = {
        code: (event.source.selected as MatOption).viewValue,
        value: event.source.value
    };

    console.log(selectedData);
}

In that 'selectedData' I am getting the code value is undefined. I need to get code value from dropdown.

The event object has "value" array attribute which will always have one item. So, instead of (event.source.selected as MatOption).viewValue , you can do event.value[0] . Look at the screenshot for more details. Hope it helps.

在此处输入图像描述

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