2017-01-06 37 views
0

我想插入一个简单的过滤器到我的项目,但它不工作,没有错误...只是不工作。我复制和简单的角JS过滤器不工作

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

这是行不通的....为什么?

感谢您的帮助

+0

你有什么错误吗? –

+2

删除一个'|'。它应该是'{{400 | ordinal}}' – devqon

+0

真棒@devqon ...只需将您的答案放在下面,我会将其标记为已回答 – MehdiN

回答

1

你要删除的一个|

{{400 | ordinal}} 

becouse 400 doesn't匹配任何“否则,如果”你没有返回在任何情况下你不会得到任何东西反正。