簡體   English   中英

無法在ngModel中獲得Angular 2下拉當前值

[英]Not able to get Angular 2 dropdown current value in ngModel

我想基於ngModel值獲取選定的值下拉列表:

<div class="form-group">
                  <div>
                  <label class="dropdown" for="profile">Profile <span style="color:red">*</span></label>
                  </div>
                  <div class="half">
                  <select class="form-control" name='profile' [(ngModel)]='users.profile._id' required>
                        <option *ngFor="let obj of profiles"  [value]="obj._id">{{obj.profile}}</option>
                  </select>
                  </div>
            </div>

NgModel指令用於下拉列表時,應始終使用NgValue指令而不是列表項(選項)的value

<select class="form-control" name='profile' [(ngModel)]='users.profile._id' required>
    <option *ngFor="let obj of profiles" [ngValue]="obj._id">
        {{obj.profile}}
    </option>
 </select>

這應該可以解決您的問題。

感謝@GünterZöchbauer和@Stefan Svrkota

我以前將this.users.profile=''初始化為字符串,這是我的錯誤,應該將其初始化為this.users.profile={}對象,其余代碼仍在工作。

暫無
暫無

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

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