How can i inject dependencies like a service into angular2 pipes?
import {Pipe, PipeTransform} from 'angular2/core';
import {MyService} from './service';
//How i am injecting MyService to the pipe?
@Pipe({name: 'exponentialStrength'})
export class ExponentialStrengthPipe implements PipeTransform {
transform(value:number, args:string[]) : any {
return Math.pow(value, parseInt(args[0] || '1', 10));
}
}
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],
}