【发布时间】:2015-07-26 02:37:28
【问题描述】:
我正在开发一个 Ionic/Cordova 应用程序,在处理三星设备的数字输入时遇到了一些问题,他们只是 don't show decimal separator when the input type is number。如果它是本机应用程序,我可以使用 numberDecimal 类型,但是,我在 HTML 中找不到类似的输入类型。为了解决这个问题,我决定创建一个简单的 Angularjs 指令,这样掩码可以自动放置小数点分隔符:
angular.module('number.mask', [])
.directive('numberMask', function () {
return {
restrict: 'A',
require: 'ngModel',
link: function (scope, element, attrs, ctrl) {
element.on('focus',function(){
this.value = this.value;
});
element.on('keyup',function(event){
if (ctrl.$isEmpty(ctrl.$viewValue)) {
return ctrl.$viewValue;
}
var newValue=ctrl.$viewValue;
if(event.keyCode=== 8)
newValue/=10;
else if(newValue.toString().indexOf('.')===-1 && newValue.toString().indexOf(',')===-1)
newValue/=100;
else
newValue*=10;
newValue=parseFloat(Math.round(newValue * 100) / 100).toFixed(2);
ctrl.$setViewValue(newValue);
ctrl.$render();
});
}
};
});
我快到了,但是,当我按下退格键时,什么都没有触发。所有其他按钮,包括粘贴按钮,至少会触发 keydown 和 keyup,但退格键不会触发这些按钮和按键操作。所以问题是,当在 Android 虚拟键盘中点击退格按钮时,如何监听事件?
【问题讨论】:
标签: javascript android angularjs cordova ionic