MehdiN
MehdiN

Reputation: 351

Simple Angular JS Filter not working

I am trying to insert a simple filter into my project but it is not working, no errors...just not working. I am copying and pasting everything from

https://scotch.io/tutorials/building-custom-angularjs-filters

Filter.js

angular.module('starter.filters', [])

// Setup the filter
.filter('ordinal', function() {

  // Create the return function
  // set the required parameter name to **number**
  return function(number) {

    // Ensure that the passed in data is a number
    if(isNaN(number) || number < 1) {

      // If the data is not a number or is less than one (thus not having a cardinal value) return it unmodified.
      return number;

    } else {

      // If the data we are applying the filter to is a number, perform the actions to check it's ordinal suffix and apply it.

      var lastDigit = number % 10;

      if(lastDigit === 1) {
        return number + 'st'
      } else if(lastDigit === 2) {
        return number + 'nd'
      } else if (lastDigit === 3) {
        return number + 'rd'
      } else if (lastDigit > 3) {
        return number + 'th'
      }

    }
  }
});

dependencies

angular.module('starter', ['ionic', 'starter.controllers','starter.filters','ngCordova'])

index.html

<script src="js/app.js"></script>
<script src="js/controllers.js"></script>
<script src="js/filters.js"></script>

HTML:

{{400 || ordinal}}

This is does not work....why?

Thanks for your help

Upvotes: 0

Views: 87

Answers (1)

Hiraqui
Hiraqui

Reputation: 447

You have to remove one of the |

{{400 | ordinal}}

You won't get anything back anyways becouse the 400 doesn´t match any 'else if' and you are not returning anything in that scenario.

Upvotes: 1

Related Questions