Angular 2 dependency injection in pipes

You can inject the dependency in the constructor like this:

export class ExponentialStrengthPipe implements PipeTransform {

  constructor(public testService: TestService) {

  }

  transform(value:number, args:string[]) : any {
    // you can access this.testService here
    return Math.pow(value, parseInt(args[0] || '1', 10));
  }
}

Don’t forget to make sure you add this dependency to the app module:

@NgModule({
    declarations: [...],
    imports: [...],
    providers: [..., TestService],
    bootstrap: [AppComponent],
}

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)