Reputation: 53
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.
Upvotes: 1
Views: 493
Reputation: 7566
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>
Upvotes: 1