简体   繁体   English

角度5获取材料选择值

[英]Angular 5 get Material Select Value

I'd like to get mat-option value from mat-select inside mat-form-field using form submit, how to return mat-option value? 我想使用表单提交从mat-form-field内的mat-select中获取mat-option值,如何返回mat-option值?

My HTML File : 我的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);
}

You need to use [(ngModel)] 您需要使用[(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>

and access it on submit 并在提交时访问

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM