根据货币格式货币面具指令,飞角,输入字段 [英] Angular, input field with a currency mask directive for money format on the fly

查看:136
本文介绍了根据货币格式货币面具指令,飞角,输入字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用一个欧盟货币领域创建一个输入掩码 http://jquerypriceformat.com/

I'm trying to create an input mask for a EU money field using http://jquerypriceformat.com/

所以在我的指令为止,输入正确地显示与蒙版的应用的用户,但我相信有什么不对的,因为POST值正在与怪异的格式,比我们在输入栏中看到完全不同的发送。

So far in my directive, the input shows correctly to the user with the mask applied, but I believe there's something wrong, because the POST values are being sent with weird formatting, totally different than what we see in the input field.

我包括priceformat.js

I include the priceformat.js

<script src="js/jquery.price_format.1.8.min.js"></script>

<input type="text" currency-input ng-model...>

和对角:

app.directive('currencyInput', function() {
    return {
      require: '?ngModel',
      link: function($scope, element, attrs, controller) {
        element.priceFormat({
            prefix: '',
            centsSeparator: ',',
            thousandsSeparator: '.'
        });
      }
    };
});

我的输入准确地显​​示与面罩的价值,但在POST数据(通过角调用)这是一个不同的值,我错过什么?

My input shows the value with the mask correctly, but on POST data (called by angular) it's a different value, what am I missing?

输入> 2.200,80 |后> 22,0080

input > 2.200,80 | post > 22,0080

感谢

推荐答案

从你的榜样,我没有看到该链接返回的东西。

From your example I don't see that link returns something.

我会写指令是这样的:

.directive('format', ['$filter', function ($filter) {
    return {
        require: '?ngModel',
        link: function (scope, elem, attrs, ctrl) {
            if (!ctrl) return;


            ctrl.$formatters.unshift(function (a) {
                return $filter(attrs.format)(ctrl.$modelValue)
            });


            ctrl.$parsers.unshift(function (viewValue) {

          elem.priceFormat({
            prefix: '',
            centsSeparator: ',',
            thousandsSeparator: '.'
        });                

                return elem[0].value;
            });
        }
    };
}]);

演示1 <大骨节病> 小提琴

如果你想在启动消防过滤器,使用 $格式化

If you want on start fire the filter, use $formatters:

现在链接是:

link: function (scope, elem, attrs, ctrl) {
            if (!ctrl) return;

            var format = {
                    prefix: '',
                    centsSeparator: ',',
                    thousandsSeparator: ''
                };

            ctrl.$parsers.unshift(function (value) {
                elem.priceFormat(format);

                return elem[0].value;
            });

            ctrl.$formatters.unshift(function (value) {
                elem[0].value = ctrl.$modelValue * 100 ;
                elem.priceFormat(format);
                return elem[0].value;
            })
        }

演示2 <大骨节病> 小提琴

这篇关于根据货币格式货币面具指令,飞角,输入字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆