简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM