パイプ プロバイダーをコンポーネントに送信することでこれを処理し、変換メソッドを実行します。そしてAngular 9で動作します。誰かの役に立てば幸いです! デモ: https://stackblitz.com/edit/angular-kdqc5e
パイプインジェクター.コンポーネント.ts:
import { Component, OnInit, Input, PipeTransform } from '@angular/core';
@Component({
selector: 'pipe-injector',
template: `
Should inject my pipe provider
{{ getText() }}
`,
providers: []
})
export class PipeInjectorComponent {
@Input() pipeProvider: PipeTransform;
@Input() pipeArgs: Array<any>;
@Input() textToFormat: string;
getText() {
return this.pipeProvider.transform(this.textToFormat, ...this.pipeArgs);
}
}
app-component.ts:
import { Component, OnInit } from '@angular/core';
import { DatePipe } from '@angular/common';
@Component({
selector: 'app-root',
template: `
<pipe-injector [pipeProvider]="pipeProvider" [pipeArgs]="pipeArgs" textToFormat='05-15-2020'>
</pipe-injector>
`,
providers: []
})
export class AppComponent implements OnInit {
pipeArgs = ['dd/MM/yyyy'];
constructor(public pipeProvider: DatePipe) {}
}
app.module.ts:
import { DatePipe } from '@angular/common';
import { PipeInjectorComponent } from './pipe-injector.component';
@NgModule({
declarations: [
PipeInjectorComponent,
],
imports: [
],
providers: [
DatePipe,
],
bootstrap: [AppComponent]
})
export class AppModule { }