auto-complete icon indicating copy to clipboard operation
auto-complete copied to clipboard

How to set a default value from a object?

Open KevinBeckers opened this issue 8 years ago • 3 comments

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">

KevinBeckers avatar Apr 21 '17 12:04 KevinBeckers

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;
    });
}

allenhwkim avatar Apr 25 '17 17:04 allenhwkim

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);
        }

itsashish-patel avatar Feb 09 '18 13:02 itsashish-patel

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

almothafar avatar Mar 14 '18 10:03 almothafar