首页 > 解决方案 > 数据绑定到组件后如何触发函数或什么事件?

问题描述

我在我的 angular 10 项目中使用 angular material select。

这是模板:

<mat-form-field appearance="fill">
      <mat-select [(ngModel)]="defaultSelection" (selectionChange)="onSelect($event)">
      <mat-option *ngFor="let name of enumKeys" [value]="name">
        {{enum[name]}}
      </mat-option>
    </mat-select>
  </mat-form-field>
  

这是组件:

export class EnumSelectionComponent implements OnInit {

  @Input() enum: Object = Status;
  defaultSelection = Status.Info;
  enumKeys=[];

  constructor() { 
    this.enumKeys=Object.keys(this.enum);  
  }

  onSelect(selection) { 
    alert(selection)
  }

  ngOnInit(): void {}
  
}

在 *ngFor 指令中生成选择选项后,我需要触发一个函数。

我的问题是我怎么知道选项是在 *ngFor 指令中生成的,我是否有任何迹象表明我可以运行一些事件处理程序?

标签: angulartypescriptwebangular-material

解决方案


推荐阅读