ng2-ui / auto-complete

Angular Auto Complete component and directive
http://ng2-ui.github.io/auto-complete/
MIT License
280 stars 124 forks source link

How to set a default value from a object? #190

Closed KevinBeckers closed 1 month ago

KevinBeckers commented 7 years ago

I have te following input. I would like to fill the input on init. But when I do the input will display [object Object] instead of customer.name. When autocomplete the customer name is displayed properly. So the problem only occurs with the init method.

ngOnInit() {
this._invoiceService.getInvoice(this.invoice.id).subscribe(result => {
      this.customer = result.customer;
    });
}
<input type="text" ng2-auto-complete 
[(ngModel)]="customer" list-formatter="name" [source]="clientModel">
allenhwkim commented 7 years ago

this.customer is an object which has [object Object] as the string representation. When autocomplete, it sets toString() to display properly. Thus, please set toString() to display it properly.

This is an example.

ngOnInit() {
this._invoiceService.getInvoice(this.invoice.id).subscribe(result => {
      this.customer = result.customer;
      this.customer.toString = () => this.customer.name;
    });
}
itsashish-patel commented 6 years ago

Hi @allenhwkim, Good day to you. want to create a new pull request to solve the mentioned issue. it can be solved adding the following condition in the ngOnInit function and it resolves an issue that a form control value converted from object to string for a selected value. please review below plunker to reproduce the issue Plunker

else if (!!this.formControl && this.formControl.value) {
            if(this.displayPropertyName)
                this.selectNewValue(this.formControl.value[this.displayPropertyName]);
             else
                 this.selectNewValue(this.formControl.value);
        }
almothafar commented 6 years ago

@itsashish-patel you may add pull request I think, thanks.

almothafar commented 1 month ago

This issue is stale. Please consider opening a new issue with details for the latest version if you are still facing the same issue or need improvement.

Closing it.