data binding failed in ng2-select

放肆的年华 提交于 2019-12-12 17:34:34

问题


I am trying to bind array of object to dropdown using ng2-select.It works fine when I tried using array of string

private category: Array<object> = [{ "value": 1, "text": "Table" }, { "value": 2, "text": "Chair" }, { "value": 3, "text": "Light"}]

and my html as follow:

 <ng-select [items]="category" [allowClear]="true"
                                       placeholder="No country selected">
                            </ng-select>

I have also Imported selectModule in my module.ts


回答1:


Format of your data is not correct.

Instead of:

private category: Array<object> = [
    { "value": 1, "text": "Table" },
    { "value": 2, "text": "Chair" },
    { "value": 3, "text": "Light" }
]

Use:

private category: Array<object> = [
    { "id": 1, "text": "Table" },
    { "id": 2, "text": "Chair" },
    { "id": 3, "text": "Light" }
]

The difference is in value which represents key of one item. This i ofcourse defined by ng-select module developer.



来源:https://stackoverflow.com/questions/43543745/data-binding-failed-in-ng2-select

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!