I'm trying to create a re-usable pipe that takes a string in it's constructor. But I can't figure out how to get it to work.
I get an error on the constructor parameter: No suitable injection token for parameter 'replacement' of class 'CustomBooleanPipe'. Consider using the @Inject decorator to specify an injection token.(-992003) custom-boolean.pipe.ts(8, 28): This type is not supported as injection token.
Here is the custom pipe:
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'customBoolean',
standalone: true
})
export class CustomBooleanPipe implements PipeTransform {
private replacement: string;
constructor(replacement: string) {
this.replacement = replacement;
}
transform(object: any, keyName: string, ...args: unknown[]): string {
return object[keyName] ? this.replacement : '';
}
}
The pipe is put in an object and passed to child components through an input variable, and used in the html.
<div *ngFor="let column of gridColumns" [matColumnDef]="column.name">
<th mat-header-cell *matHeaderCellDef> {{ column.name }} </th>
<td mat-cell *matCellDef="let element; let i = index" class="compact-form" [ngClass]="column.style">
<div> {{ column.dataPipe ? column.dataPipe.transform(element, column.dataKey) : element[column.dataKey] }}</div>
</td>
</div>
gridColumns is the input variable of type IGridColumn[]
export interface IGridColumn {
name: string;
dataKey: string;
style?: string;
dataPipe?: PipeTransform
sortKey?: string;
sortOrder?: SortDirection;
}
gridColumns: IGridColumn[] = [
{
name: 'Beta',
dataKey: 'betafeature',
style: 'emphasized-cell',
dataPipe: new CustomBooleanPipe('BETA')
},
{
name: 'Paid',
dataKey: 'paidfeature',
style: 'emphasized-cell',
dataPipe: new CustomBooleanPipe('$')
}
];
Its much simpler to pass it as an input to the transform method, since it reduces the number of objects created and you can vary the input based on the params being sent. Please check the below example!
pipe
ts
stackblitz