Search code examples
javascriptangularjsangularjs-filter

Replace integer with word


I receive numbers for the movie genres from an api. Which means, 1 = Action, 2 = Drama, 3 = Horror .... I want to display the genre name instead of the number.How can i do that?

 {{::movie.genre}}

movies have multiple genres so that has to works too. movie genre: 1&2

Can i use a filter like this ?

{{::today | date}}

Thanks in advance


Solution

  • https://docs.angularjs.org/guide/filter

    .filter('genre', function() {
      return function(input) {
        input = input || '';
        var out = '';
        switch(input) {
          case 1:
            out = 'horror';
            break;
          case 2:
            out = 'comedy';
            break;
          default:
            out = input;
        }
        return out;
      };
    })