我正在尝试使用'this‘关键字从filterfunction()调用一个方法。然而,我意识到'this‘指的是事件处理程序而不是组件,并且我为'this’获得的值为空,因此我得到了一个运行时错误。
export class SmartTableComponent {
settings = {
...
columns: {
...
firstName: {
title: 'First Name',
type: 'string',
filterFunction(cell?: any, search?: string): boolean {
return this.doFilter(cell, search);
}
},
...
},
...
};
doFilter(cell?: any, search?: string): boolean{
return true;
}
}
在Java中,我们将通过使用SmartTableComponent.this.doFilter(...)获得对'this‘的引用。但这似乎在TypeScript中不起作用。
如何从ng2- filterFunction -table中调用组件的方法?
发布于 2018-06-02 03:26:49
似乎,通过使用lambda表达式而不是匿名函数,“this”的值是从包装器类中维护的。所以,这就是解决方案:
export class SmartTableComponent {
settings = {
...
columns: {
...
firstName: {
title: 'First Name',
type: 'string',
filterFunction:(cell?: any, search?: string) => {
return this.filterByText(cell.doc[0]['value'], search);
},
},
...
},
...
};
doFilter(cell?: any, search?: string): boolean{
return true;
}
}
https://stackoverflow.com/questions/50581664
复制相似问题