Learner Learner - 4 months ago 7x
AngularJS Question

In angular how to write strings based on condition of numbers

I have

{{ index+1 }}

I need to display "one" if number is 1 in the expression above.

Display "two" if number is 2 in the expression above and so on up to 6
Display "six" if number is 6.

Since I need only these 1 to 6, I do not prefer to go to JavaScript based number to word format conversion and using it as filter in the above expression. Because, the grunt reports heaps of problem in that solution, so I am looking for a simple conditional display only for this boundary between 1-6.


I would do this using a filter, but you can use a function as well.

An example of a function would be:


which would call this and give 'one'

    var smallNumbers = ["zero", "one", "two", "three", "four", "five", "six"];
    vm.convertToWord = function(input) {
      input = parseInt(input, 10);
      if (input < smallNumbers.length) {
        return smallNumbers[input];
      } else {
        return "not valid";

See this Codepen for an example using a filter and and a function.