【发布时间】:2017-01-09 21:31:48
【问题描述】:
我正在使用 ng-repeat 循环浏览项目列表。有一些值我想显示为简单的数字,如“435”或“43”,一些我想显示为双精度数字,如小数点后 2 位“545,32.43”或“343.32”,一些值是我想要的百分比显示为“75%”,有些是我想显示为“$65,434”的货币。
我知道 angularjs 为各种操作(数字、货币、百分比)等提供过滤器。
但是我怎样才能根据条件动态选择其中一个过滤器呢?
javascript
config:[
{key:"Issue County",value:50,valColor:'#4caeed', type:'integer',dec_places:0},
{key:"Issue Average", value:10.5356 ,valColor:'#4caeed', type:'double', dec_places:2},
{key:"Issues Percentage", value:57, valColor:'#e54343', type:'percentage', dec_places:2},
{key:"Total Amount", value:65000, valColor:'#4ca977', type:'currency', dec_places:0}
]
html
<ui class="subCardDiv" ng-repeat="conf in item.config">
<li style="display: inline;">
<span class="cardDivKey" style="padding-top: 2px;">{{conf.key}} </span>
<span class="cardDivVal" ng-style="{color:conf.valColor}" style="padding-top: 2px;">{{conf.value | number:conf.dec_places}} </span>
<span ng-if="conf.text" class="cardDivText" style="padding-top: 2px;">{{conf.text}} </span>
</li>
</ui>
【问题讨论】:
-
构建您自己的过滤器并将
conf对象传递给它。注入$filter服务并在需要时调用适当的过滤器(请参阅this answer 了解如何执行此操作)。
标签: javascript html angularjs angularjs-ng-repeat angular-filters