【问题标题】:ngx-select-dropdown patch value with id带有 id 的 ngx-select-dropdown 补丁值
【发布时间】:2022-01-27 02:32:10
【问题描述】:

你好吗? 使用 ngx select 时出现以下问题。 我有以下对象,employee = {id: 20, name: "Company 1", "status": "enabled"}。 始终在选项中选择以显示名称,然后当我返回时发送 ID。但是当我得到这个值时,ngx 选择会丢失,无法进行绑定并获取公司名称。 使用正常的 html 选择工作正常。我将在下面留下两个示例。

雇员配置= { 显示键:'名称', 搜索:真, 高度:'自动', placeholder: '选择一个位置', limitTo: this._cargos.length, moreText: '项目', noResultsFound: '未找到结果', searchPlaceholder: '搜索', searchOnKey: '名字', };

【问题讨论】:

  • 你能格式化代码吗?为了更好的可读性。谢谢。

标签: javascript angular typescript


【解决方案1】:

来自ngx-select-dropdown documentation[options] 需要数组值。当您将 employe 传递给 [options] 时,它是一个对象。

[options]="employe"

您需要将candidates 传递给[options]

<ngx-select-dropdown
  [config]="employeConfig"
  [options]="candidates"
  name="employe"
  [(ngModel)]="employe"
  required
>
</ngx-select-dropdown>

更新:

来自 ngx-select-dropdown 库和演示,当[options] 的值是对象数组时,它似乎返回选定对象。并且不支持像displayKey配置一样指定id作为返回选择值。

仅获取选定的 id:

let selectedId = this.employe.id;

Sample Demo on StackBlitz

【讨论】:

猜你喜欢
  • 2020-04-03
  • 2019-01-22
  • 2020-05-06
  • 1970-01-01
  • 2021-05-12
  • 2015-07-17
  • 2021-06-06
  • 2019-05-12
  • 2016-12-21
相关资源
最近更新 更多