角度材质选择,selectionChange 未触发

chi*_*rag 5 angular-material angular

我准备了示例(角度材质选择):

斯塔克闪电战

我已为 mat-select 设置了 ngOnInIt 的值,但SelectionChange 事件未触发。每当我使用 value 属性将 value 设置为 mat-select 时,我希望触发 SelectionChange,并且我想对 SelectionChange 做一些工作。

SelectionChange 在手动用户选择时触发,但在设置值时不触发。

为什么不触发选择更改事件?

还有其他事件或方法可以实现这一目标吗?

pc_*_*der 4

演示selectionChange事件仅在用户更改 html 元素(而不是后端数据更改)时触发。您可以使用Viewchild手动调用函数的方式:

@ViewChild("changeEl") el!: ElementRef;

    ngAfterViewInit(){
        this.selectedFood = this.foods[0].value;
        this.selectionChangeTax(this.el)
      }
Run Code Online (Sandbox Code Playgroud)

HTML:

<h4>Basic mat-select</h4>
<mat-form-field appearance="fill">
  <mat-label>Favorite food</mat-label>
  <mat-select (selectionChange)="selectionChangeTax($event)" #changeEl [(value)]="selectedFood">
    <mat-option *ngFor="let food of foods" [value]="food.value">
      {{food.viewValue}}
    </mat-option>
  </mat-select>
</mat-form-field>
Run Code Online (Sandbox Code Playgroud)