在Angular 2+中的ngx-bootstrap Typeahead中绑定对象而不是字符串

Pra*_*dhi 6 typeahead ngx-bootstrap angular

我正在使用链接来实现Typeahead。现在,我使用TypeaheadOptionField在typeahead上显示名称,但它也将名称字符串绑定在模型中。我想绑定对象而不是字符串。

我的HTML代码:

<input formControlName="item"  class="form-control" [typeahead]="allItemsArray" [typeaheadItemTemplate]="customItemTemplate"
          [typeaheadOptionsLimit]="7" [typeaheadMinLength]="0" [typeaheadOptionField]="name" (typeaheadOnSelect)="onSelectItem($event)">
Run Code Online (Sandbox Code Playgroud)

allItemsArray:

[
    {
        name: 'a',
        code: '12'
    },
    {
        name: 'b',
        code: '13'
    }
]

Value bound to form control: 'a'
Required value: {'name': 'a', 'code': '12'}
Run Code Online (Sandbox Code Playgroud)

我尝试做的一件事是实现一个事件,该事件将模型值设置为对象,但没有用。

Man*_*uel 1

我现在遇到了完全相同的问题。

当 typeahead 与 Angular Form API 一起使用时,它使用为传入的键找到的值typeaheadOptionField。恕我直言,这是一个错误,或者至少它应该是可配置的。

我现在正在做的是用自定义控件包装输入,并使用typeaheadOnSelect在选择预输入选项时调用的输出。在事件数据中,您可以找到整个对象。但你必须自己处理控制数据管理。

至少目前我找不到其他解决办法。

编辑:

这是我的代码(删除了所有抽象,不保证它有效):

@Component({
  selector: 'my-typeahead-control',
  templateUrl: './my-typeahead-control.html',
  providers: [
    {provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => MyTypeaheadControl), multi: true}
  ]
})
export class MyTypeaheadControl implements ControlValueAccessor
{
  // -- -- -- -- -- -- -- -- -- -- typeahead data -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

  @Input()
  public items:any[] | Observable<any[]> = [];

  @Input()
  public itemLabelKey:string;

  // -- -- -- -- -- -- -- -- -- -- internal data -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

  public selectedItemLabel:string;

  // -- -- -- -- -- -- -- -- -- -- interface implementation -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

  public writeValue(obj:any):void
  {
    this.updateSelectedItemLabel(obj);
  }

  private onChange:Function;

  public registerOnChange(fn:any):void
  {
    this.onChange = fn;
  }

  private onTouch:Function;

  public registerOnTouched(fn:any):void
  {
    this.onTouch = fn;
  }

  public setDisabledState(isDisabled:boolean):void
  {
    // ...
  }

  // -- -- -- -- -- -- -- -- -- -- control data handling -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

  public onSelect(event:TypeaheadMatch):void
  {
    this.onTouch();
    this.onChange(event.item);
    this.updateSelectedItemLabel(event.item);
  }

  private updateSelectedItemLabel(obj:any):void
  {
    this.selectedItemLabel = (this.itemLabelKey) ? _.get(obj, this.itemLabelKey) : obj;
  }
}
Run Code Online (Sandbox Code Playgroud)

和模板:

<input [ngModel]="selectedItemLabel"

       [typeahead]="items"
       [typeaheadOptionField]="itemLabelKey"
       [typeaheadMinLength]="0"
       [container]="'body'"

       (typeaheadOnSelect)="onSelect($event)">
Run Code Online (Sandbox Code Playgroud)

现在可以按如下方式使用:

    <my-typeahead-control formControlName="item" [items]="allItemsArray" itemLabelKey="name"></my-typeahead-control>
Run Code Online (Sandbox Code Playgroud)