jx1986
jx1986

Reputation: 53

Replacing strings in expression Angular 4

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

Answers (2)

Phil
Phil

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

CornelC
CornelC

Reputation: 5254

You need to use pipes.

Check out the documentation

Upvotes: 0

Related Questions