简体   繁体   English

如何以角度6获取ngSelect下拉列表的选定文本和选定值

[英]How to get Selected Text and Selected Value of an ngSelect dropdown in angular 6

When form is posted, I want to get selected item Name and Value both of an ngSelect dropdown in angular 6. 当发布表单时,我想获得角度为6的ngSelect下拉列表中的选定项目名称和值。

I have tried getting it with ngModel and with templateVariable, but Selected Name is not returned, I only managed to get Selected Value. 我试过用ngModel和templateVariable来获取它,但是没有返回Selected Name,我只设法获得Selected Value。

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

For a list shown in dropdown {Mumbai :1}, {Pune: 2}, {Delhi: 3}, if I select Pune, then I should get "Pune" as well as "2" as output json. 对于下拉列表中显示的列表{孟买:1},{Pune:2},{德里:3},如果我选择Pune,那么我应该将“Pune”和“2”作为输出json。

I've not used that package before, but I was curious based on your question, and I think I have an answer for you. 我之前没有使用过那个包,但是根据你的问题我很好奇,我想我有一个答案。

If you look here https://github.com/ng-select/ng-select it indicates in the API Inputs section that 如果你看这里https://github.com/ng-select/ng-select它表明在API输入部分

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

So I would guess that if you omit the bindValue property, you will have the entire object rather than just the Id. 所以我猜想如果省略bindValue属性,你将拥有整个对象而不仅仅是Id。

Also noticed that there is an event you can hook into 还注意到有一个事件可以挂钩

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


Question author indicated was not able to get the below to work 问题作者指出无法让以下工作


so you could do something like this. 所以你可以做这样的事情。

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

assuming that you want to set a name property on model as well as the Id. 假设您要在模型上设置名称属性以及Id。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在单击 angular 7 中的按钮时获取下拉列表的选定值和选定文本 - How to get selected value and selected text of a dropdown on click a button in angular 7 角度-如何获得在下拉菜单中选择的值? - Angular - how to get value selected in dropdown menu? 在 angular 中,如何获取选中的文本以及下拉选中项的值? - In angular, how to get both the selected text as well as value of the dropdown selected item? 如何从页面加载的下拉列表中获取选定的值和文本,然后单击具有 angular 8 响应形式的按钮 - How to get the selected value and text from a dropdown on page load and on click a button with reactive form in angular 8 如何在加载时获得角度6下拉列表的选定值(不更改) - How to get selected value of dropdown in angular 6 on load(not onchange) 如何在angular2的表单元素中获取下拉列表选择的值 - How to get dropdown list selected value in form element in angular2 如何在angular2中获取多个select下拉选择值 - How to get multi select dropdown selected value in angular2 从Angular 2中的选定下拉列表中获取值 - Get value from selected dropdown list in Angular 2 如何在 Angular 8 中为 ngSubmit() 获取选定的下拉值? - How to take selected dropdown value in Angular 8 for ngSubmit()? 如何将选定的值绑定到 Angular 4 中的下拉列表 - How to bind selected value to dropdown list in Angular 4
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM