0

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

3
  • Do you get any errors? Commented Jan 6, 2017 at 20:43
  • 2
    Remove one |. It should be {{ 400 | ordinal }} Commented Jan 6, 2017 at 20:44
  • Awesome @devqon ...just put your answer below and I'll mark it as answered Commented Jan 6, 2017 at 21:09

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.