Home > Software engineering >  replace a string by another string custom pipe
replace a string by another string custom pipe

Time:02-06

I have a requirement to create custom pipe for replacing a string to another. If the string is 'true' then i have to display it as 'Yes'. If the string is 'false' then i have to display it as 'No'. I tried creating a custom pipe for it , but its not working.

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

@Pipe({ name: 'replaceText' })

export class ReplaceTextPipe implements PipeTransform {
    transform(value: string): string {       
        if (value) {
            console.log(value);
            if (value === "true") {
                value = value.replace("true", "Yes");               
            } else {
                value = value.replace("false", "No");                
            }          
            return value;
        }
    }
   
}

Can any one guide me please.

CodePudding user response:

I think the problem could be that you are passing boolean value as string.

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

@Pipe({
  name: 'replaceText'
})
export class ReplaceTextPipe implements PipeTransform {
  transform(value: boolean): any {
    return value ? "Yes" : "No";
  }
}

Is work progressing: {{isRunning | replaceText }}
  •  Tags:  
  • Related