I am facing an issue with using $formatters.
My objective is to conceal the phone number, while displaying only the last 4 characters. It should not display anything if left blank. If text is entered, the model is impacted by the mask and the hidden phone number is saved in the database...
Below is the directive I am utilizing:
.directive('tsHideField', function () {
return {
require: 'ngModel',
restrict: 'A',
link: function (scope, element, attributes, controller) {
var maskValue = function (value) {
if (!value) {
return "";
}
if (value.length <= 4) {
return value;
}
var valueHide = "";
if (value.indexOf('@') === -1) {
//leave last 4 chars
valueHide = value.toString().substring(0, value.length - 4).replace(/[\S]/g, "\u2022");
return valueHide + value.toString().substring(value.length - 4);
} else {
//Email address, keep after @ and hide everything except the last 4 letters before
valueHide = value.toString().substring(0, value.indexOf('@') - 4).replace(/[\S]/g, "\u2022");
return valueHide + value.toString().substring(value.indexOf('@') - 4);
}
}
controller.$formatters.push(function (value) {
return maskValue(value);
});
}
};
});
To aid you further, here's a fiddle with a small implementation: http://jsfiddle.net/nqp4qtLk/2/
How can I prevent the model from being affected by the mask?
EDIT: I modified Gr3g's answer to align with my requirements
Check out the updated fiddle here: Updated fiddle