oferh / ng2-completer

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

Cannot read property 'search' of undefined #300

Open lkarun opened 7 years ago

lkarun commented 7 years ago

Im using following code to load data from webservice.

@Component({
    selector: 'pi-names',
    template: `
    <ng2-completer #box  (keyup)="onEnter(box.value)" [(ngModel)]="searchStr" [datasource]="dataPINameService" [minSearchLength]="0"></ng2-completer>
    `
})

export class PiNames extends KeyUpComponent{

protected searchStr: string;
protected dataService: CompleterData;

constructor(private completerService: CompleterService, private dataService : DataService) {
        super();
}

onEnter(value: string) { 
    this.dataServicePINames.getOndemandData(this.searchStr).subscribe((pinames) => {
            this.pinames = JSON.parse(pinames).recordset;
            console.log(this.pinames);
    });

    let timedRes = Observable.from([this.pinames]).delay(3000);
    this.dataService = this.completerService.local(timedRes, 'first_name', 'first_name');
}

im still getting 'ERROR TypeError: Cannot read property 'search' of undefined' error. Please help. Thanks,

oferh commented 7 years ago

@lkarun to customize the search you should provide a custom data provider see a sample in the demo you can ofcourse use dataServicePINames instead of issuing an http request. also using keyup to do the search instead of ng2-completer will probably not work.