Angular 4 中 md-select 中的 onselected 事件

2024-04-28

我想在使用 md-select 选择值时调用打字稿中的函数。在材料设计中用于此目的的属性是什么?

   <md-select placeholder="State">
       <md-option *ngFor="let state of states" [value]="state.code">{{ state.name }}
   </md-option>

对于材质版本 >= 6

Use the (selectionChange)事件于mat-select.

<mat-form-field>
    <mat-select placeholder="State" (selectionChange)="someMethod($event.value)">
        <mat-option *ngFor="let state of states" [value]="state.value">
            {{ state.viewValue }}
        </mat-option>
    </mat-select>
</mat-form-field>

在事件方法中,$event.value包含当前选择的[value]。参考官方文档 https://material.angular.io/components/select/api.

@Output() SelectionChange: EventEmitter

当用户更改所选值时发出事件。

这是一个链接Stackblitz 演示 https://stackblitz.com/edit/angular-6-stackoverflow-9wpyd8?file=src%2Fapp%2Fcomponents%2Fstackoverflow-solution%2Fstackoverflow-solution.component.html.


对于材质版本

Use the (change)输出事件。

<md-select placeholder="State" (change)="someMethod()">
  <md-option *ngFor="let state of states" [value]="state.value">
    {{ state.viewValue }}
  </md-option>
</md-select>

另一种方法是使用(onSelectionChange)事件于<md-option>:

<md-select placeholder="State">
  <md-option *ngFor="let state of states" [value]="state.code" 
             (onSelectionChange)="anotherMethod($event, state)">
    {{ state.name }}
  </md-option>
</md-select>

Link to Stackblitz 演示 https://stackblitz.com/edit/angular-kzhjqm?file=app%2Fapp.component.html.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Angular 4 中 md-select 中的 onselected 事件 的相关文章

随机推荐