Angular ng lightning-查找时未定义数据对象
我正在使用Lookup组件,但得到一个错误,即我的数据对象未定义,因此无法使用.filter()。代码如下:Angular ng lightning-查找时未定义数据对象,angular,ng-lightning,Angular,Ng Lightning,我正在使用Lookup组件,但得到一个错误,即我的数据对象未定义,因此无法使用.filter()。代码如下: getAllAccounts() { this._quickAddService.getAllAccounts() .subscribe( accounts => this.getAllAccountsFinished(accounts), error => this.errorMessage = <any>
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);
}
getAllAccounts(){
这是._quickAddService.getAllAccounts()
.订阅(
accounts=>this.getAllAccountsFinished(accounts),
error=>this.errorMessage=error);
}
getAllAccountsFinished(帐户:任意){
这个账户=账户;
console.log(this.accounts);
this.hideSpinner();
}
恩戈尼尼特(){
这个.getAllAccounts();
}
查找(查询:字符串):帐户[]{
如果(!查询){
返回null;
}
返回this.accounts.filter((item)=>item.name.toLowerCase().indexOf(query.toLowerCase())>-1);
}
该console.log显示一旦服务完成返回,数据已正确绑定。但是,当在输入时启动查找时,此.accounts未定义。由上的@bekos回答。需要向组件构造函数添加绑定:
constructor(elementRef:ElementRef, private _quickAddService:QuickAddService) {
this.visible = true;
this.lookup = this.lookup.bind(this);
}
关于这一点,我只做一点小小的评论;-) 在TypeScript中包装查找方法而不是使用
bind
方法可能更好,因为这样会丢失类型检查
大概是这样的:
this.lookup = (query) => {
this.lookup(query);
};
有关更多详细信息,请参阅此链接:
lookup
?它是绑定到模板中元素的属性:快速搜索帐户
,这就是为什么创建@Output()lookup=new EventEmitter()通常在角度上更好的原因
中的
并像@GunterZochbauer一样绑定到它是的,但在我们的例子中,有一些“体操”我不想让用户处理;)我懂了。看起来很合理。谢谢你的回馈!是的,这是我的怀疑:)谢谢你的快速反应!最重要的是:)我试过了,说我得等两天才能找到自己的答案/