使用AngularJS添加两个十进制数

时间:2022-09-15 11:49:00

I need to update an input such that if is an integer, you add 2 zeros.

我需要更新输入,如果是整数,则添加2个零。

Input: 12 Output: 12.00 (In the same input-text)

输入:12输出:12.00(在相同的输入文本中)

<body ng-app="">
    <script>
    function Controller($scope) {
      $scope.change = function() {

      };
    }
  </script>
  <div ng-controller="Controller">
    <input type="text" ng-model="value" ng-change="change()" />
    {{value | number:2}}
  </div>
</body>

It could be done within $scope.change, but it would take more lines of code. Is there a more direct way to implement it? such as {{value | number 2}}.

它可以在$ scope.change中完成,但需要更多行代码。有更直接的方式来实现它吗?例如{{value | 2号}}。

Thanks in advance.

提前致谢。

1 个解决方案

#1


2  

I think you've got it. Angular provides a default filter number. It works as:

我想你已经明白了。 Angular提供默认过滤器编号。它的作用如下:

{{val | number:<fractionsize>}}

If this number will represent currency, consider using the currency filter.

如果此数字代表货币,请考虑使用货币过滤器。

Edit: Using a filter also probably is a good idea for maintainability of your code, as it separates getting the data from formatting it.

编辑:使用过滤器也可能是代码可维护性的好主意,因为它将数据与格式化分开。

#1


2  

I think you've got it. Angular provides a default filter number. It works as:

我想你已经明白了。 Angular提供默认过滤器编号。它的作用如下:

{{val | number:<fractionsize>}}

If this number will represent currency, consider using the currency filter.

如果此数字代表货币,请考虑使用货币过滤器。

Edit: Using a filter also probably is a good idea for maintainability of your code, as it separates getting the data from formatting it.

编辑:使用过滤器也可能是代码可维护性的好主意,因为它将数据与格式化分开。