I have a problem using $ formatters.
My goal is to hide the phone number, just leave the last 4 characters visible. This is normal if you are not writing anything in the input. If you write something, the mask affects the model, and I register the hidden phone in DB ...
In this directive, I use:
.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 { //Adresse email, on laisse après le @ et on cache tout sauf les 4 dernières lettre avant //' lambertjer@gmail.com '.substring(0,' lambertjer@gmail.com '.indexOf('@') - 4).replace(/[\S]/g, "\u2022") + ' lambertjer@gmail.com '.substring(' lambertjer@gmail.com '.indexOf('@') - 4) valueHide = value.toString().substring(0, value.indexOf('@') - 4).replace(/[\S]/g, "\u2022"); return valueHide + value.toString().substring(value.indexOf('@') - 4); } // replace all characters with the mask character //return (value || "").replace(/[\S]/g, "\u2022"); } /** SI ON VEUT EGALEMENT CACHER AL ECRIT: * * var createMaskedInputElement = function() { if (! maskedInputElement || ! maskedInputElement.length) { maskedInputElement = element.clone(true); maskedInputElement.attr("type", "password"); // ensure the value is masked maskedInputElement.removeAttr("name"); // ensure the password save prompt won't show maskedInputElement.removeAttr("core.application.main.directive.mask"); // ensure an infinite loop of clones isn't created maskedInputElement.bind("blur", function() { element.removeClass("ng-hide"); maskedInputElement.remove(); maskedInputElement = null; }); $compile(maskedInputElement)(scope); element.after(maskedInputElement); } }; element.bind("focus", function() { createMaskedInputElement(); element.addClass("ng-hide"); maskedInputElement[0].focus(); }); */ controller.$formatters.push(function (value) { return maskValue(value); }); } }; });
And for your object, here is a fiddle with a small implementation: http://jsfiddle.net/nqp4qtLk/2/
How to prevent the model from affecting the mask?
EDIT: I will adapt Gr3g's answer to my requirements
see updated script: Updated violin
source share