How to use services in custom pipes in angular 2

Viewed 2337

I want to display months in years on html. My filter takes number of months as an input and it should return years.

For example: if it takes 12 as input, it should return translated value as 1 year. and if it is more than 1 year, it should return '2 years'.

my pipe contains:

import { Pipe, PipeTransform} from '@angular/core';

@Pipe({name : 'displayMonthsInYears '})
export class DisplayMonthsInYears implements PipeTransform{
    transform(noOfMonths : number) : any {
         if(noOfMonths > 12){
            return (noOfMonths / 12 + $translate.instace('YEARS'));
         }
         return (noOfMonths / 12 + $translate.instace('YEAR'));
    }
}

and html is :

 <div class="text-left">
     {{ contractDuration | displayMonthsInYears }}
 </div>

I don't have a clue how to make that happen. Please help.

1 Answers
Related