簡體   English   中英

如何在角度顯示墊選的選擇選項?

[英]How to show selected option of mat-select in angular?

在這里,我在該下拉菜單中選擇的下拉菜單中有多個用戶,當我選擇任何用戶時,我想顯示此所選用戶名,但是在ngModel中,我有userObject.userid,我不想更改顯示任何所選用戶名的方式。

chat.component.html

<mat-form-field style="width: 25%;margin-top: 20px;margin-left: 20px;">
  <mat-select placeholder="Select User" [(ngModel)]="userObject.userid" name="userid" required>
    <mat-option *ngFor="let item of userObj" [value]="item.userid">
      {{item.username}}
    </mat-option>
  </mat-select>
</mat-form-field>  

<h2>Selected : {{userObject.username}}</h2>

我只是將整個對象綁定到select,然后使用一個變量,例如selectedUser 顯然,您需要userid ,但是它安全地存儲在變量以及username 所以我該怎么做...(還更改一些變量名以區分它們):

<mat-select placeholder="Select User" [(ngModel)]="selectedUser" name="userid" required>
  <mat-option *ngFor="let userObj of users" [value]="userObj">
    {{userObj.username}}
  </mat-option>
</mat-select>

{{selectedUser.userid}}, {{selectedUser.username}}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM