1

i have a json file of fruits name which is like

[
    { "fruits" : "orange"
    },
    { "fruits" : "apple"
    }
]

and angular a normal

<div>{{fruits}}</div>

how do i change the strings in results to other words, example i want the orange become orens and apple becomes apel without modifying json file. I have more than 30 words to be replaced.

2 Answers 2

1

This sounds like the perfect use-case for a pipe!

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

@Pipe({name: 'fruitMapper'})
export class FruitMapperPipe implements PipeTransform {
  transform(value: string): string {
    let result: string = value;
    // reassign result in a switch-block
    return result;
  }
}

And then in your template

<div>{{fruits | fruitMapper}}</div>
Sign up to request clarification or add additional context in comments.

Comments

0

You need to use pipes.

Check out the documentation

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.