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();
});
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.
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));
});