0

我需要对选项选择做出反应。我的问题是,当我选择选项 A 时,此组件未发出事件(selectionChange输出未发出新事件),我打开选择窗格并再次选择选项 A。该事件未触发,因为选择未更改,但我需要在我的用例中触发选择更改事件。有人知道该怎么做吗?有没有办法selectionModel在这个mat-select组件中覆盖?我有一个链接列表,选择其中任何一个后,即使用户再次选择相同的链接,我也需要做出反应。我正在使用@angular/core@5.2.5@angular/material@5.2.1

这是我要使用的带有此组件的模板:

<mat-form-field>
    <mat-select placeholder="Search history"
        [disabled]="(links | async).length === 0"
        [value]="(links | async).length > 0 ? (links | async)[0] : null"
        (selectionChange)="onSearchHistorySelect($event.value)">
        <mat-option *ngFor="let l of links | async" [value]="l">{{l.title}}</mat-option>
    </mat-select>
</mat-form-field>
4

1 回答 1

0

You can add a click event to your option, so the function will be called even if the option is already selected.

<mat-form-field>
    <mat-select [(ngModel)]="status">
      <mat-option *ngFor="let option of options" [value]="option.value (click)="myFunction()">
        {{ option.name }}
      </mat-option>
    </mat-select>
</mat-form-field>

The only problem I found with this is that you can't give the value of the selected option to the function (or at least I didn't find a way). But you can work around that by binding the value to a property in your class with ngModel.

Then in your function you can just call this property (in my case this.status).

myFunction() {
  console.log(this.status);  // Do stuff with your selected value
}

Hope this is what you are looking for.

于 2018-06-04T13:54:38.497 回答