oferh / ng2-completer

Angular 2 autocomplete component
http://oferh.github.io/ng2-completer/
MIT License
347 stars 172 forks source link

Unable to use object with ng2-completer submitted forms #258

Closed asahu8 closed 7 years ago

asahu8 commented 7 years ago
 <ng2-completer 
    [(ngModel)]="searchStr? searchStr.name : '' " 
    [datasource]="dataService" 
    [minSearchLength]="0" 
    (selected)="onSelected($event)"
    formControlName="country"
    [class]="form-control">
  </ng2-completer>
protected onSelected(selectedItem: CompleterItem) {
    if(selectedItem) {
      this.selectedCountry = selectedItem.originalObject;
      this.searchStr = this.selectedCountry;
  }
}

With above code in UI it display the selected country's name in the UI and when the form is submitted it sends the same string.

I expect it to send the country object. In the UI on using "searchStr" instead of "searchStr.name" it submits the object. But in UI it displays [object Object].

Tried using ngValue but am not sure ng2-completer supports it or not. Please help!

oferh commented 7 years ago

is this the same question as this stackoverflow question?

iamonuwa commented 7 years ago

<ng2-completer formControlName="captain" (selected)="onSelected($event)" [datasource]="captains" [minSearchLength]="0"></ng2-completer>

onSelected(event){ console.log('Event:', JSON.stringify(event)) }

asahu8 commented 7 years ago

Yeah something similar I did & it worked for me. Closing this.