如何获取角度6中的ngSelect下拉菜单的选定文本和选定值

时间:2019-06-04 11:43:40

标签: angular angular-ngselect

发布表单后,我想获取角度6中的ngSelect下拉列表的选定项名称和值。

我尝试使用ngModel和templateVariable来获取它,但是没有返回“所选名称”,我只设法获取了“所选值”。

<ng-select
 name="testFunctionID"
 [items]="testFunctions"
 [multiple]="false"
 bindLabel="name"
 bindValue="testFunctionID"
 [(ngModel)]="model.testFunctionID">
</ng-select>

对于下拉列表{Mumbai:1},{Pune:2},{Delhi:3}中显示的列表,如果选择Pune,则应将“ Pune”和“ 2”作为输出json。 / p>

1 个答案:

答案 0 :(得分:0)

我以前没有使用过该软件包,但是基于您的问题我感到很好奇,我想我有一个答案。

如果您在这里https://github.com/ng-select/ng-select,则在“ API输入”部分中指出

bindValue - Object property to use for selected model. By default binds to whole object.

所以我猜想,如果省略bindValue属性,则将拥有整个对象,而不仅仅是ID。

还注意到,您可以加入一个事件

(change) - Fired on model change. Outputs whole model


问题作者表示无法使以下内容正常工作


所以你可以做这样的事情。

<ng-select
 name="testFunctionID"
 [items]="testFunctions"
 [multiple]="false"
 bindLabel="name"
 bindValue="testFunctionID"
 [(ngModel)]="model.testFunctionID"
 (change)="model.testFunctionName = $event.name">
</ng-select>

假设您要在模型以及ID上设置名称属性。