Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

131
Views
Angular 2 dependency injection in pipes

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));
  }
}
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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],
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!