角指令四舍五入的十进制数

编程入门 行业动态 更新时间:2024-10-18 16:46:48
本文介绍了角指令四舍五入的十进制数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

角是否有一个内置的指令圆输入值?

Does Angular have a built-in directive to round the input value?

因为我要轮NG-模型中的实际 VAL 以及数过滤器是不是在这种情况下,适当的。如果我有写一个,那将是什么样子?

The number filter is not appropriate in this case because I want to round the actual val in ng-model as well. If I have to write one, what would it be like?

推荐答案

您可以使用指令创建双向绑定的转换。这里有一个简单的例子:

You can create two way binding conversion using directives. Here's a quick example:

app.directive('roundConverter', function() { return { restrict: 'A', require: 'ngModel', link: function(scope, elem, attrs, ngModelCtrl) { function roundNumber(val) { var parsed = parseFloat(val, 10); if(parsed !== parsed) { return null; } // check for NaN var rounded = Math.round(parsed); return rounded; } // Parsers take the view value and convert it to a model value. ngModelCtrl.$parsers.push(roundNumber); } }; });

plnkr.co/edit/kz5QWIloxnd89RKtfkuE?p=$p$ PVIEW

这真的取决于你想要的工作。它应该限制用户输入?你介意模型和视图的价值有什么不同?如果无效的数字输入是在模型空?这些都是决定你做。该指令将上述无效数字转换成空。

It really depends on how you want it to work. Should it restrict user input? Do you mind having the model and view value differ? Should invalid number inputs be null on the model? These are all decisions for you to make. The directive above will convert invalid numbers to null.

更多推荐

角指令四舍五入的十进制数

本文发布于:2023-11-10 02:41:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1574115.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:指令   四舍五入   十进制数

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!