我应该使用过滤器将整数值转换成百分比吗?

时间:2022-09-15 11:44:48

I need to collect a rate of change - in percentages - from my application's users. Here is the text input I am using:

我需要从我的应用程序的用户那里收集更改率(百分比)。这里是我正在使用的文本输入:

<label for="annual-change" class="pt-underline"> I am anticipating
<input id="annual-change" ng-model="calc.yrChange"  type="text" placeholder="0" /> % of growth annually.<br><br>
</label>

Now what I want is to use a filter that takes the integer amount that the user inputs and convert it to a percentage by multiplying it by 0.01 or dividing it by 100 before I send it to the controller for calculations

现在我想要使用一个过滤器它将用户输入的整数量转换成百分数乘以0。01或者除以100在我将其发送给控制器进行计算之前

But I just can't figure out where to place the filter and how to hook it. So I tried it with a directive like so:

但我就是想不出在哪里放置过滤器,以及如何将其挂起。所以我尝试了这样一个指令:

    app.directive("percent", function($filter){
    var p = function(viewValue){
      console.log(viewValue);
      var m = viewValue.match(/^(\d+)/);
      if (m !== null)
        return $filter('number')(parseFloat(viewValue)/100);
    };

    var f = function(modelValue){
        return $filter('number')(parseFloat(modelValue)*100);
    };

    return {
      require: 'ngModel',
      link: function(scope, ele, attr, ctrl){
          ctrl.$parsers.unshift(p);
          ctrl.$formatters.unshift(f);
      }
    };
});

This kind of works but shouldn't I be using a filter for this task? How do I do this?

这种方法是有效的,但是我不应该为这个任务使用过滤器吗?我该怎么做呢?

2 个解决方案

#1


4  

Well, you just did it the absolute right way with the ctrl.$parser and ctrl.$formatter You just can leave out the $filter thing, its absulotuly not needed there. Just check it out they don't use the filter there, too.

用ctrl键完全正确。解析器和ctrl美元。$formatter你可以省去$filter这个东西,它在这里根本不需要。检查一下,那里也没有过滤器。

#2


0  

I think you should use a directive. If you think you are about to fall back to using JQuery to start manipulating the DOM elements then a directive is probably what you want.

我认为你应该使用指令。如果您认为您将退回到使用JQuery开始操作DOM元素,那么指示可能就是您想要的。

This is an example of a filter: http://embed.plnkr.co/TT6ZwOF6nYXwMcemR3JF/preview

这是一个过滤器示例:http://embed。plnkr.co/tt6zwof6nyxwmcemr3jf/preview

What you could do is filter the value you enter as part of a watch event. However that depends on your specific use case.

您可以做的是过滤您作为监视事件的一部分输入的值。然而,这取决于您的特定用例。

#1


4  

Well, you just did it the absolute right way with the ctrl.$parser and ctrl.$formatter You just can leave out the $filter thing, its absulotuly not needed there. Just check it out they don't use the filter there, too.

用ctrl键完全正确。解析器和ctrl美元。$formatter你可以省去$filter这个东西,它在这里根本不需要。检查一下,那里也没有过滤器。

#2


0  

I think you should use a directive. If you think you are about to fall back to using JQuery to start manipulating the DOM elements then a directive is probably what you want.

我认为你应该使用指令。如果您认为您将退回到使用JQuery开始操作DOM元素,那么指示可能就是您想要的。

This is an example of a filter: http://embed.plnkr.co/TT6ZwOF6nYXwMcemR3JF/preview

这是一个过滤器示例:http://embed。plnkr.co/tt6zwof6nyxwmcemr3jf/preview

What you could do is filter the value you enter as part of a watch event. However that depends on your specific use case.

您可以做的是过滤您作为监视事件的一部分输入的值。然而,这取决于您的特定用例。