我如何在前端使用名称并在选定时发送 id

问题描述 投票:0回答:1
<input *ngIf="showAutoComplete"
       [(ngModel)]="selected"
       [typeahead]="uniqueCreatedBys"
       [typeaheadOptionField]="'name'"
       class="form-control"
       (ngModelChange)=
       "ObjectType('',userId =  selected.id )">

我正在使用 ngx bootstarp 的 typehead,需要在前端部分访问名称,但在 ObjectType 函数中发送 id,我的 uniqueCreatedBys 包含名称和 id 的列表

//这是我的功能

ObjectType(q?: string, userId? : number, offset?: number, limit?: number) {
    // console.log(this.objectTypes)
    // const userId = this.selected ? this.objectTypes.find(obj => obj.createdBy === this.selected)?.createdById : undefined;
    this.objectTypeService.fetchObjectTypes(q, userId , offset, limit).subscribe((response: any) => {
      this.objectTypeService.resetObjectTypes();
      this.objectTypeService.setObjectTypes(response);

      this.objectTypes = this.objectTypeService.getObjectTypes();
      this.totalCount = this.objectTypeService.getTotalDocsCount();

      this.uniqueCreatedBys = this.getUniqueCreatedBys(this.objectTypes);
      console.log(this.uniqueCreatedBys)
    });
  }
angular development-environment bootstrap-typeahead
1个回答
0
投票

而不是

ngModelChange
您可以使用 onSelect 之类的东西

<input *ngIf="showAutoComplete"
       [(ngModel)]="selected"
       [typeahead]="uniqueCreatedBys"
       [typeaheadOptionField]="'name'"
       class="form-control"
       (typeaheadOnSelect)=
       "ObjectType('',selected?.id )">

工作演示

© www.soinside.com 2019 - 2024. All rights reserved.