最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angular Material mat-select set default selected value - Stack Overflow

programmeradmin3浏览0评论

I am using Angular 7 and Angular Material. I want my to have a default value, but it is not working as expected.

 <mat-select required [(value)]="userProfile.gender">
    <mat-option value="Male">Male</mat-option>
    <mat-option value="Female">Female</mat-option>
  </mat-select>

userProfile does have a property and the value is Male in my test case, but nothing is being selected in the html. What am I doing wrong?

I am using Angular 7 and Angular Material. I want my to have a default value, but it is not working as expected.

 <mat-select required [(value)]="userProfile.gender">
    <mat-option value="Male">Male</mat-option>
    <mat-option value="Female">Female</mat-option>
  </mat-select>

userProfile does have a property and the value is Male in my test case, but nothing is being selected in the html. What am I doing wrong?

Share Improve this question asked Jan 2, 2019 at 17:14 dshukertjrdshukertjr 18.8k11 gold badges70 silver badges101 bronze badges 1
  • Can you post the typescript code ? – Niladri Commented Jan 2, 2019 at 17:21
Add a ment  | 

1 Answer 1

Reset to default 2

I don't know why, but changing [(value)] to [(ngModel)] solved it

  <mat-select required [(ngModel)]="userProfile.gender">
    <mat-option value="Male">Male</mat-option>
    <mat-option value="Female">Female</mat-option>
  </mat-select>
发布评论

评论列表(0)

  1. 暂无评论