在Angular的Select选项中获取所选对象 [英] Getting the Selected Object in Select Option in Angular

查看:350
本文介绍了在Angular的Select选项中获取所选对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个放置在选择选项上的对象数组,我想在选择选项中选择一个对象列表.但是我无法获取该对象列表的值.输出[对象] [对象].我想获取该公司对象的选定列表.我将其分配给[值],它得到[对象] [对象].

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)
}

推荐答案

尝试这样:

添加ngModel供您选择,并使用 ngModelChange 代替更改.

add ngModel for your select and use ngModelChange instead of change.

  1. 添加ngModel
  2. 使用ngModelChange而不是更改.
  3. 使用[ngValue]代替[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

Component.ts

assignCorporationToManage(selectedValue) {
     console.log(selectedValue)
}

这篇关于在Angular的Select选项中获取所选对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆