Scipion Scipion - 3 months ago 55
TypeScript Question

Angular2 rc.6 Use pipe in component

In the rc.5 I used to do in my component :

private myCustomPipe = new CustomPipe()
this.myCustomPipe.transform(...)


Since I did migrate the application however, I am having an error on new CustomPipe() (I did move the injection from my component to my module's declarations).

In app.module.ts :

@NgModule({
declarations: [CustomPipe],
imports: [
BrowserModule,
FormsModule,
HttpModule
],
bootstrap: [AppComponent],
})
export class AppModule {}


Any idea how to achieve this in the Rc.6 ?

Answer

If you want to use it in code (component, service, ...) not only in the view then you need to add it to providers

@NgModule({
    declarations: [CustomPipe], // only for view
    providers: [CustomPipe], // to inject to components or services
    imports: [
        BrowserModule,
        FormsModule,
        HttpModule
    ],
    bootstrap: [AppComponent],
})
export class AppModule {}