简单的Angular JS Filter无法正常工作

时间:2017-01-06 20:39:05

标签: javascript angularjs ionic-framework filter

我正在尝试在我的项目中插入一个简单的过滤器,但它不起作用,没有错误......只是没有工作。我正在复制并粘贴

中的所有内容

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'
      }

    }
  }
});

依赖

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}}

这不起作用....为什么?

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

你必须删除其中一个|

{{400 | ordinal}}

你不会得到任何反馈,因为400不匹配任何'else if'而你在这种情况下你没有返回任何东西。

相关问题