[英]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.