I am just starting out with Angular.
Currently, I need to assign a method to my paginator.getRangeLabel (I want to use either a standard label or a suffixed one depending on certain conditions):
this.paginator._intl.getRangeLabel = this.getLabel;
The code above is a simplified version that works fine, but it involves some repetitive logic in both methods. The actual code is quite lengthy, so here's a shortened version for clarity:
private getLabel(page: number, pageSize: number, length: number): string {
return 'some result';
}
private getLabelWithSuffix(page: number, pageSize: number, length: number): string {
return 'some result ...';
}
I aim to refactor this code to incorporate a shared method and minimize redundancy. Below are the implementations of the methods:
private getLabel(page: number, pageSize: number, length: number): string {
return this.combineStrings(page, pageSize, length, false);
}
private getLabelWithSuffix(page: number, pageSize: number, length: number): string {
return this.combineStrings(page, pageSize, length, true);
}
private combineStrings(page: number, pageSize: number, length: number, suffix: boolean): string {
return 'some result' + (suffix ? '...' : '');
}
However, when I try to run this code, an exception is displayed in the console:
ERROR TypeError: this.test is not a function at MatPaginatorIntl.getLabel [as getRangeLabel] (xxx.ts:21:21) at MatPaginator_Template (paginator.mjs:329:1469) at executeTemplate (core.mjs:9622:1) at refreshView (core.mjs:9488:1) at refreshComponent (core.mjs:10659:1) at refreshChildComponents (core.mjs:9284:1) at refreshView (core.mjs:9538:1) at refreshComponent (core.mjs:10659:1) at refreshChildComponents (core.mjs:9284:1)