问题描述
我所创建的指令使用功能setFormatting在输入字段来掩盖文本值。
The directive I have created uses the function setFormatting to mask the text value in an input field.
scope.$watch(element, function() {
modelCtrl.$setViewValue(setFormatting(element.val(), attrs.symbol));
modelCtrl.$render();
});
element.bind('blur', function() {
modelCtrl.$setViewValue(setFormatting(element.val(), attrs.symbol));
modelCtrl.$render();
});
的范围。$手表适用于当该内容被加载/施加首次掩模,对element.bind适用于其它时间的掩模。的范围。$手表存储符号(如果存在的话),为毫微克模型变量的一部分。该element.bind不是。我以为$ setViewValue()和$渲染()没有更新NG-模型变量。哪里是可变正在更新?
The scope.$watch applies the mask when the content is loaded/applied the first time, the element.bind applies the mask for the other times. The scope.$watch is storing the symbol (if there is one) as part of the ng-model variable. The element.bind is not. I thought $setViewValue() and $render() did not update the ng-model variable. Where is the variable being updated?
请参阅附小提琴:
感谢。
See attached fiddle: http://jsfiddle.net/PJ3M4/
Thanks.
推荐答案
我能够通过添加modelCtrl。$ parsers.push(获取NG-模型我想要的方式存储值){...}我的范围。$腕表(){...}。
I was able to get the ng-model to store values the way I wanted to by adding a modelCtrl.$parsers.push() { ... } to my scope.$watch() { ... }.
scope.$watch(element, function() {
modelCtrl.$parsers.push(function(inputValue) {
showAlert("Watch", 1);
if (!prev) {
prev = false;
var returnVal = checkVal(inputValue, modelCtrl, decimals, true, minVal, maxVal);
if (String(returnVal) === ".") {
setAndRender(modelCtrl, "");
return "";
}
else {
return returnVal;
}
}
return String(inputValue).replace(/[^0-9 . -]/g, '');
});
prev = true;
setAndRender(modelCtrl, setFormatting(element.val(), decimals, attrs.prefix, attrs.symbol));
});
这篇关于AngularJS指令数字格式掩蔽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!