简体   繁体   English

AngularJs:如何在ngRepeat内添加自定义过滤器来操作小数?

[英]AngularJs: How to add a custom filter inside an ngRepeat to manipulate decimals?

I have a single object of multiple property names and values that are being printed out via ng-repeat. 我有多个属性名称和值的单个对象,这些对象将通过ng-repeat打印出来。 Two of the objects inside this large object, are called currencyName and currencyPrice. 这个大对象中的两个对象称为currencyName和currencyPrice。

By default, these objects have 8 decimals hardcoded into them in the backend, and my task is to manipulate certain currencyName to show 5 decimals, or 8 decimals in the display page with Angular. 默认情况下,这些对象在后端有8个十进制的硬编码,我的任务是操纵某些currencyName以显示5个十进制,或者在Angular显示页面中显示8个十进制。 By default, the currencyPrices have 8 decimals hardcoded into them on the backend, which cannot be touched since that bit of code is reused in other places. 默认情况下,currencyPrices的后端有8个小数位硬编码,无法触摸,因为那部分代码已在其他地方重用。 For the display page I'm working on, we use the filter | 对于我正在处理的显示页面,我们使用过滤器| number:2 by default. 数量:默认为2。

I tried to create a custom filter that will target the array of the main object, run a for loop/forEach method, target the currencyName object, and if it matches an array that contains currencyNames to be manipulated, the angular display page will show 5 decimals (in this case). 我试图创建一个自定义过滤器,该过滤器将主要对象的数组作为目标,运行for循环/ forEach方法,将currencyName对象作为目标,如果它与包含要操作的currencyNames的数组匹配,则角度显示页面将显示5小数点(在这种情况下)。

This has proven very difficult to me. 这对我来说非常困难。 I don't have the full code to paste it here, but here's some sample code to show what the code looks. 我没有完整的代码将其粘贴到此处,但是这里有一些示例代码来显示代码的外观。

<tr ng-repeat="full in fulls.mainList" class="animate-repeat">
  <td>{{full.currencyName}}</td>
  <td>{{full.tradedAverage | number:2}}</td>
  <td>{{full.tradedAmount | number:2}}</td>
  <td>{{full.currencyPrice | number:2}}</td>
</tr>

fulls in this case is the large object that's being parsed inside the controller. 在这种情况下,fulls是控制器内部正在解析的大对象。

Tested your code and it should work: 测试了您的代码,它应该可以工作:

   <div ng-repeat="full in fulls.mainList">
       <span>{{full.currencyName}}</span>
       <span>{{full.tradedAverage | number:2}}</span>
       <span>{{full.tradedAmount | number:2}}</span>
       <span>{{full.currencyPrice | number:2}}</span>
   </div>

    $scope.fulls = {
          mainList : [
            {
              currencyName: 'TEST',
              tradedAverage: '12.123434',
              tradedAmount: '13.133434',
              currencyPrice: '14.143434'
            }
          ]
       };

http://jsfiddle.net/Lvc0u55v/1/ http://jsfiddle.net/Lvc0u55v/1/

Maybe you have ',' instead of '.' 也许您有','而不是'。 in your numbers? 在你的号码?

Note it's pretty easy to write your own function in your controller that encapsulates the logic for the number of decimals. 注意,在控制器中编写自己的函数非常简单,该函数封装了小数位数的逻辑。

<tr ng-repeat="full in fulls.mainList" class="animate-repeat">
  <td>{{full.currencyName}}</td>
  <td>{{full.tradedAverage | number: (fnThatTellsTheNumberOfDecimals(full, 'tradedAverage')}}</td>
  <td>{{full.tradedAmount | number:(fnThatTellsTheNumberOfDecimals(full, 'tradedAmount')}}</td>
  <td>{{full.currencyPrice | number:(fnThatTellsTheNumberOfDecimals(full, 'currencyPrice')}}</td>
</tr>

Then in your controller, write something like: 然后在您的控制器中,编写如下内容:

$scope.fnThatTellsTheNumberOfDecimals = function(element, attr){
    if (attr === 'example' && element.type === 'some'){ return 2; }
    return 6;
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM