¿Cómo puedo inyectar dependencias como un servicio en tuberías angulares 2?
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)); } }Puede inyectar la dependencia en el constructor de esta manera:
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)); } }No olvides asegurarte de agregar esta dependencia al módulo de la aplicación:
@NgModule({ declarations: [...], imports: [...], providers: [..., TestService], bootstrap: [AppComponent], }