我有一个输入字段接受ex:2的整数,而离开该字段时它应自动格式化为2.00

时间:2017-10-17 16:47:14

标签: angular ecmascript-6

我正在使用只接受数字的指令。 我有一个输入字段,接受ex:2的整数,而离开该字段时,它应自动格式化为2.00。

const isValid = value => _.isEmpty(value) || (new RegExp(/^(\d{0,6})(\.
(\d{1,2}))?$/)).test(value);

function link(scope, elem, attr, ctrl) {
/* @desc Will process each time the value is updated by the user. */
ctrl.$parsers.unshift((value) => {
const valid = isValid(value);
ctrl.$setValidity('decimalNumber', valid);
return valid ? value : undefined;
});

/* @desc Will process each time the value is updated on the DOM element. */
ctrl.$formatters.unshift((value) => {
ctrl.$setValidity('decimalNumber', isValid(value));
return value;
});
}

1 个答案:

答案 0 :(得分:0)

您可以在输入字段中使用(blur)事件并将其绑定到方法以格式化值。 alert("about to call exec in getInfo"); exec(successCallback, errorCallback, "Device", "getDeviceInfo", []); alert("after exec in getInfo");