在输入字段点替换逗号 [英] Replace comma with a dot in input field
本文介绍了在输入字段点替换逗号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
欧洲国家使用逗号符号(,)而不是一个点(。),当他们进入小数。所以,我想,当用户输入的输入用逗号更换点的迹象。我知道,输入=号做到这一点,但我需要IE浏览器的支持。
我猜指令是最好做到这一点?我把它与下面的code一试。但它失败。
.directive('replaceComma',函数(){
返回{
限制:'A',
更换:真实,
链接:功能(范围,元素,ATTRS){
范围。$腕表(attrs.ngModel,功能(五){
变种转换=字符串(ⅴ).replace(,,。);
attrs.NgModel =转换;
});
}
}
});
转换变量是正确的。但该值并不在输入框中变化。所以我想attrs.ngModel =转换,有错吗?
解决方案
通过指令:
.directive('replacecomma',函数(){
返回{
要求:'ngModel',
链接:功能(范围,元素,ATTRS,ngModelCtrl){
范围。$腕表(attrs.ngModel,功能(的newval){
如果(==的newval不确定和放大器;!&安培;!的newval == NULL){
。ngModelCtrl $ setViewValue(字符串(的newval).replace(/,/克)'。');
element.val(字符串(的newval).replace(/,/克)'。');
}
}) }
}
});
European countries uses a comma-sign (,) instead of a dot (.) when they enter decimal numbers. So I want to replace the dot sign with a comma when users enter input. I'm aware that input=number do this but I need support for IE.
I guess directive is the best to do this? I gave it a try with the code below. But it fails.
.directive('replaceComma', function(){
return {
restrict: 'A',
replace: true,
link: function(scope, element, attrs){
scope.$watch(attrs.ngModel, function (v) {
var convert = String(v).replace(",",".");
attrs.NgModel = convert;
});
}
}
});
The convert variable is correct. But the value does not change in the input box. So I guess attrs.ngModel = convert, is wrong?
解决方案
Via directive:
.directive('replacecomma', function () {
return {
require: 'ngModel',
link: function (scope, element, attrs, ngModelCtrl) {
scope.$watch(attrs.ngModel, function (newVal) {
if (newVal !== undefined && newVal !== null) {
ngModelCtrl.$setViewValue(String(newVal).replace(/,/g, '.'));
element.val(String(newVal).replace(/,/g, '.'));
}
})
}
}
});
这篇关于在输入字段点替换逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文