Angular 5 get Material Select Value

匿名 (未验证) 提交于 2019-12-03 01:04:01

问题:

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

My HTML File :

  <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); } 

回答1:

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> 

and access it on submit

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


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!