ng-lightning - data object is undefined on lookup

◇◆丶佛笑我妖孽 提交于 2019-12-31 04:28:10

问题


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.


回答1:


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



回答2:


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:

  • https://basarat.gitbooks.io/typescript/content/docs/tips/bind.html


来源:https://stackoverflow.com/questions/37213908/ng-lightning-data-object-is-undefined-on-lookup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!