【发布时间】: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