我正在尝试使用 ng2-select 将对象数组绑定(bind)到下拉列表。当我尝试使用字符串数组时它工作正常
private category: Array<object> = [{ "value": 1, "text": "Table" }, { "value": 2, "text": "Chair" }, { "value": 3, "text": "Light"}]
我的html如下:
<ng-select [items]="category" [allowClear]="true"
placeholder="No country selected">
</ng-select>
我还在我的 module.ts 中导入了 selectModule
最佳答案
您的数据格式不正确。
而不是:
private category: Array<object> = [
{ "value": 1, "text": "Table" },
{ "value": 2, "text": "Chair" },
{ "value": 3, "text": "Light" }
]
用途:
private category: Array<object> = [
{ "id": 1, "text": "Table" },
{ "id": 2, "text": "Chair" },
{ "id": 3, "text": "Light" }
]
区别在于value
,它代表一项的键。这当然是由 ng-select 模块开发人员定义的。
关于angular - ng2-select 中的数据绑定(bind)失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43543745/