【问题标题】:Simple Angular JS Filter not working简单的 Angular JS 过滤器不起作用
【发布时间】:2017-05-21 16:54:40
【问题描述】:

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

的所有内容

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

过滤器.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}}

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

感谢您的帮助

【问题讨论】:

  • 你有什么错误吗?
  • 删除一个|。应该是{{ 400 | ordinal }}
  • 真棒@devqon ...只需将您的答案放在下面,我会将其标记为已回答

标签: javascript angularjs ionic-framework filter


【解决方案1】:

您必须删除其中一个 |

{{400 | ordinal}}

因为 400 不匹配任何“else if”并且在这种情况下您不会返回任何东西,所以您无论如何都不会得到任何回报。

【讨论】:

    猜你喜欢
    • 2013-11-08
    • 1970-01-01
    • 1970-01-01
    • 2014-11-30
    • 1970-01-01
    • 1970-01-01
    • 2018-03-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多