在 Angular 的选择选项中获取所选对象 [英] Getting the Selected Object in Select Option in Angular
本文介绍了在 Angular 的选择选项中获取所选对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个对象数组,我把它放在选择选项上,我想在选择选项中选择一个对象列表.但是我无法获得该对象列表的值.它输出[对象] [对象].我想获得公司对象的选定列表.我将它分配给 [value],它得到 [object][Object].
<select class="form-control col-lg-8" #selectedValue (change)="assignCorporationToManage(selectedValue.value)"><option *ngFor="let Corporation of user_corporations" [value]="corporation">{{corporation.corp_name}}</option></选择>
<块引用>
ts
assignCorporationToManage(selectedValue) {console.log(selectedValue)}
解决方案
试试这个:
为您的选择添加 ngModel 并使用 ngModelChange 而不是更改.
- 添加 ngModel
- 使用 ngModelChange 而不是更改.
- 使用 [ngValue] 而不是 [value].
component.html
<select class="form-control col-lg-8" #selectedValue name="selectedValue" id="selectedValue" [(ngModel)]="selectedValue" (ngModelChange)="assignCorporationToManage($事件)"><option *ngFor="let company of user_corporations" [ngValue]="corporation">{{corporation.corp_name}}</option></选择>
组件.ts
assignCorporationToManage(selectedValue) {console.log(selectedValue)}
I have this array of objects which I put on the select option and I would want to select one list of object in the select option. However I can't get the value of that list of object. It outputs [object] [Object]. I want to get that selected list of object of the corporation. I assigned it to the [value] and it gets [object][Object].
<div class="select" >
<select class="form-control col-lg-8" #selectedValue (change)="assignCorporationToManage(selectedValue.value)">
<option *ngFor="let corporation of user_corporations" [value]="corporation">{{corporation.corp_name}}</option>
</select>
</div>
ts
assignCorporationToManage(selectedValue) {
console.log(selectedValue)
}
解决方案
Try like this :
add ngModel for your select and use ngModelChange instead of change.
- adding ngModel
- using ngModelChange instead of change.
- using [ngValue] instead of [value].
component.html
<select class="form-control col-lg-8" #selectedValue name="selectedValue" id="selectedValue" [(ngModel)]="selectedValue" (ngModelChange)="assignCorporationToManage($event)">
<option *ngFor="let corporation of user_corporations" [ngValue]="corporation">{{corporation.corp_name}}</option>
</select>
Component.ts
assignCorporationToManage(selectedValue) {
console.log(selectedValue)
}
这篇关于在 Angular 的选择选项中获取所选对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文