下拉列表未填充json对象 [英] dropdown is not populating with json object
问题描述
我的ng-model值是-"{" Id:1," Description:" Draft," Code:" Draft}",而ng-options是-
my ng-model value is - "{"Id":1,"Description":"Draft","Code":"Draft"}" and ng-options are -
[
{
"Id": 1,
"Description": "Draft",
"Code": "Draft"
},
{
"Id": 2,
"Description": "UserSubmitted",
"Code": "UserSubmitted"
},
{
"Id": 3,
"Description": "PLMApproved",
"Code": "PLMApproved"
}
]
所以我的下拉列表没有填充(但值具有约束力).
so my dropdown is not populating(but value is binding).
我低估了,如果ng-model值只是一个整数值,它将被绑定,那么json对象呢?请帮助.
I understatnd, if ng-model values is just a integer value it would bind, then how about json object? Please help needed.
推荐答案
AFAIK不会绑定,因为您的模型是一个不同的对象,即指向不同的内存位置,并且与数组内的任何对象都不匹配.
AFAIK it won't bind because your model is a different object i.e. points to a different memory location and it doesn't match with any objects inside the array.
您可以在ng-options中使用as子句来选择要进入ng-model的属性,并使用该值设置ng-model将设置所选选项.
You could use the as clause in ng-options to select a property you want to get into the ng-model and setting ng-model with that value will set the selected option.
例如
<select ng-options="item.Id as item.Description for item in default.items" ng-model="default.selectedItemId">
<option value="">Select</option>
</select>
否则,您可以使用track by子句,该子句要在ng-model中获取一个属性或对象,但又想在ng-model中使用另一个属性设置选定的选项时,将在ng-model中指定跟踪by子句,即更新从代码到视图的绑定
else you could use the track by clause which is used when you want to get one property or object into your ng-model but you want to set the selected option with another property from the ng-model which will be specified in the track by clause i.e. update the binding from code to the view
例如
<select ng-options="item as item.Description for item in default.items track by item.Id" ng-model="default.selectedItem">
<option value="">Select</option>
</select>
这篇关于下拉列表未填充json对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!