角度5获取材料选择值 [英] Angular 5 get Material Select Value

查看:62
本文介绍了角度5获取材料选择值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用表单提交从mat-form-field内的mat-select中获取mat-option值,如何返回mat-option值?

I'd like to get mat-option value from mat-select inside mat-form-field using form submit, how to return mat-option value?

我的HTML文件:

  <form (submit)="updateProfileCompany($event)">
  <mat-form-field>
      <mat-select name="company" placeholder="Select Your Company" #companyValue>
      <mat-option *ngFor="let company of companies" [value]="company.id">
          {{company.perusahaan}}
      </mat-option>
    </mat-select>
  </mat-form-field>
  <div align="right">
      <button mat-raised-button >Add Company</button>
  </div>
  </form>

我在打字稿文件中的方法:

My method inside typescript file :

updateProfileCompany(e){
    console.log(e);
}

推荐答案

您需要使用 [(ngModel)]

You need to use [(ngModel)]

 <mat-select name="company" placeholder="Select Your Company" [(ngModel)]="company" #companyValue>
      <mat-option *ngFor="let company of companies">
          {{company.perusahaan}}
      </mat-option>
</mat-select>

并在提交时访问它

updateProfileCompany( ){
    console.log(this.company);
}

这篇关于角度5获取材料选择值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆