1

I'm working with the Lookup component and am getting an error that my data object is undefined and thus cannot .filter(). Code is below:

getAllAccounts() { this._quickAddService.getAllAccounts() .subscribe( accounts => this.getAllAccountsFinished(accounts), error => this.errorMessage = <any>error); } getAllAccountsFinished(accounts:any) { this.accounts = accounts; console.log(this.accounts); this.hideSpinner(); } ngOnInit(){ this.getAllAccounts(); } lookup(query: string): Account[] { if (!query) { return null; } return this.accounts.filter((item) => item.name.toLowerCase().indexOf(query.toLowerCase())>-1); } 

that console.log is showing that the data is bound properly once the service finishes returning. However when lookup is fired on input this.accounts is undefined.

5
  • Where is lookup called from? Commented May 13, 2016 at 15:30
  • it is a property bound to the element in the template: <ngl-lookup [lookup]="lookup" field="name" (pick)="onPick($event)" debounce="0" placeholder="Hint: type words"> <span nglLookupLabel>Quick search an account</span> </ngl-lookup> Commented May 13, 2016 at 15:33
  • Thats why it is usually better in Angular to create an @Output() lookup = new EventEmitter() in <ngl-lookup> and bind to it like <ngl-lookup (lookup)="lookup($event)" than passing around functions. Commented May 13, 2016 at 15:36
  • @GunterZochbauer Yes that's true, but in our case there is some "gymnastic" that I don't want to let user handle it ;) github.com/ng-lightning/ng-lightning/blob/master/src/lookups/… Commented May 13, 2016 at 16:55
  • I see. Seems reasonable. Thanks for the feefback! Commented May 13, 2016 at 16:59

2 Answers 2

2

Answered by @bekos on the Gitter. Need to add binding to component constructor:

constructor(elementRef:ElementRef, private _quickAddService:QuickAddService) { this.visible = true; this.lookup = this.lookup.bind(this); } 
Sign up to request clarification or add additional context in comments.

3 Comments

Yup, was my suspicion :)
Thanks for the quick response though! On top of it :)
I tried, says I have to wait two days to mark my own answer. :/
0

Just a small comment regarding this ;-)

It's perhaps better to wrap your lookup method instead of using the bind method in TypeScript because you will lose type checking.

Something like this:

this.lookup = (query) => { this.lookup(query); }; 

See this link for more details:

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.