I am designing a service that will utilize pipes as templates. In order to accomplish this, I require access to the registered pipes.
The final code structure should resemble the following:
@Injectable()
class MyService {
constructor(private injector: Injector) {}
// example usage 'myDate | date' or 'aVariable | uppercase'
public interpolate(text: string, params: object = {}): string {
let p: number = text.lastIndexOf('|');
if (p > -1) {
let args = [this.interpolate(text.substring(0, p))];
let pipeName = text.substr(p+1).trim();
// additional arguments retrieved from pipeName
pipe = this.getPipe(pipeName)
pipe.transform.apply(pipe, args);
} else {
// base interpolation logic
}
}
private pipeInstances: any = {}
private getPipe(pipeName) {
if (!this.pipeInstances[pipeName]) {
// obtaining the required pipe instance
this.pipeInstances[pipeName] = this.injector.get(PipesContainer).get(pipeName);
}
return this.pipeInstances[pipeName];
}
}
The challenge lies in accessing pipes directly from the injector. Pipes must be provided beforehand (once for directives and once for providers). Ideally, there should be a way to retrieve them from Angular's core structures (compiler, core modules, etc.) instead of defining a new list manually.