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