ng-lightning - 数据对象在查找时未定义
ng-lightning - data object is undefined on lookup
我正在使用 Lookup 组件,但收到一条错误消息,指出我的数据对象未定义,因此无法使用 .filter()。代码如下:
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);
}
console.log 表明服务完成返回后数据已正确绑定。但是,当对输入触发查找时,this.accounts 未定义。
@bekos 在 Gitter 上回答。需要向组件构造函数添加绑定:
constructor(elementRef:ElementRef, private _quickAddService:QuickAddService) {
this.visible = true;
this.lookup = this.lookup.bind(this);
}
关于这个的小评论;-)
在 TypeScript 中包装查找方法而不是使用 bind
方法可能更好,因为这样会丢失类型检查。
像这样:
this.lookup = (query) => {
this.lookup(query);
};
有关详细信息,请参阅此 link:
我正在使用 Lookup 组件,但收到一条错误消息,指出我的数据对象未定义,因此无法使用 .filter()。代码如下:
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);
}
console.log 表明服务完成返回后数据已正确绑定。但是,当对输入触发查找时,this.accounts 未定义。
@bekos 在 Gitter 上回答。需要向组件构造函数添加绑定:
constructor(elementRef:ElementRef, private _quickAddService:QuickAddService) {
this.visible = true;
this.lookup = this.lookup.bind(this);
}
关于这个的小评论;-)
在 TypeScript 中包装查找方法而不是使用 bind
方法可能更好,因为这样会丢失类型检查。
像这样:
this.lookup = (query) => {
this.lookup(query);
};
有关详细信息,请参阅此 link: