【问题标题】:how to pass data in form of array of objects?如何以对象数组的形式传递数据?
【发布时间】:2020-10-20 09:21:11
【问题描述】:

我在我的 angular 5 项目中使用包 ngx select dropdown 并想知道如何传递对象数组而不是字符串数组。 目前,如果我这样做,它会在下拉列表中将 [Object Object] 显示为选项。

data=[ {
        "id": "ab",
        "description": "аҧсуа"
    },
    {
        "id": "aa",
        "description": "Afaraf"
    },
    {
        "id": "af",
        "description": "Afrikaans"
    },
    {
        "id": "ak",
        "description": "Akan"
    }]
 <ngx-select-dropdown [config]="config" [options]="data" [(ngModel)]="datasel" [multiple]="false">
 </ngx-select-dropdown>
/** ngx-select-dropdown config */
    public config: any = {
        search: true,
        height: '260px',
        placeholder: 'Select',
        customComparator: () => { },
        limitTo: 10,
        moreText: 'more',
        noResultsFound: 'No results found!',
        searchPlaceholder: 'Search',
        searchOnKey: ''
    }

我想在下拉列表中和选择获取所选选项的 ID 时向用户显示描述。

【问题讨论】:

    标签: javascript angular angular5 dropdown ngx-select-dropdown


    【解决方案1】:

    我自己找到了这个问题的答案。 config.displaykey 丢失,根据要求添加该密钥使其工作。

    /** ngx-select-dropdown config */
    public config: any = {
        displayKey: "description",
        search: true,
        height: '260px',
        placeholder: 'Select',
        customComparator: () => { },
        limitTo: 10,
        moreText: 'more',
        noResultsFound: 'No results found!',
        searchPlaceholder: 'Search',
        searchOnKey: ''
    }
    

    【讨论】:

      【解决方案2】:

      如果传递了对象数组,则必须在配置中传递displayKey

      displayKey:"description"
      

      查看here

      【讨论】:

        猜你喜欢
        • 2020-07-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多