ng2-select中的数据绑定失败

时间:2017-04-21 13:16:26

标签: angular typescript angular2-routing ng2-bootstrap

我正在尝试使用ng2-select将对象数组绑定到下拉列表。当我尝试使用字符串数组时它工作正常

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

1 个答案:

答案 0 :(得分:3)

您的数据格式不正确。

而不是:

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模块开发人员定义。