angular - 如何在 Angular 5 的 Mat-Select 选项中获取所选值的 ID

标签 angular angular-material-5

How to get the id of selected option value in mat-select angular 5. 在onchangevent中只获取所选选项的值。但是如何获取所选选项值的 ID。

 client.component.html
<mat-form-field>
    <mat-select placeholder="Client*" #clientValue  (change)="changeClient($event)">
    <mat-option  *ngFor="let client of clientDetails"   [value]="client.clientName">
      {{client.clientName |  json}}
    </mat-option>
  </mat-select>
</mat-form-field>

client.component.ts file
export class Client{
     changeClient(event){
     console.log(event);
 }
}

最佳答案

这个问题是针对 Angular 5 的,但对于其他使用较新版本的 Angular 来到这里的人来说,(change) 事件不适用于 mat-select。

Angular 6 中,(change) 事件已更改为 (selectionChange)

那就是:

<mat-form-field>
    <mat-select placeholder="Client*" #clientValue  (selectionChange)="changeClient($event.value)">
    <mat-option  *ngFor="let client of clientDetails" [value]="client.id">
      {{client.clientName}}
    </mat-option>
  </mat-select>
</mat-form-field>

在组件中:

changeClient(value) {
    console.log(value);
}

来自 this answerthe documentation .

关于angular - 如何在 Angular 5 的 Mat-Select 选项中获取所选值的 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48705555/

相关文章:

javascript - 在 Angular Material 自动完成显示中绑定(bind) 'this' 使用 Angular 5

Angular 防止主机点击禁用输入参数的自定义按钮组件

javascript - 我如何使用 ngFor 来设计这个(可能是嵌套的)表

angular - 如何一次关闭使用 Angular Material 打开的所有对话框模式

javascript - Mat select click outside 不起作用,当下拉关闭

angular - 在 Angular Material 5 中创建滚动到顶部按钮

angular - Ionic 3 密码并使用 formBuilder 确认密码验证

angular - 通过路由器导出与子路由共享数据的正确方法?路由器 3.0.0-alpha.7

html - 如何导航(重定向)到 Angular 中的另一个页面?

angular - 无法绑定(bind)到 'ngModel',因为它不是 'mat-slide-toggle' 的已知属性