My Directive:
(function () {
'use strict';
wikiApp.directive('jzInput', ['$sce', function ($sce) {
var html = [
'<input type="text" ng-change="validate()" />'
];
var addSeparator = function (input, sep) {
return (input + '').replace(/(\d)(?=(\d{3})+$)/g, '$1' + sep);
};
var digitsOnly = function (input) {
return (input.toString()).replace(/[^\d]/g, "");
};
var parseInteger = function(input) {
return addSeparator(digitsOnly(input), ',');
};
return {
restrict: 'E',
template: html.join(''),
replace: true,
require: 'ngModel',
scope: {
type: '@'
},
link: function($scope, $element, $attr, ngModel) {
ngModel.$parsers.push(function(value){
var input = value;
var output = input;
// Integer
output = parseInteger(input);
console.log(output);
return output;
});
}
};
}]);
})();
My HTML:
<jz-input type="text" ng-model="test">
<input type="text" ng-model="test">
The input has the updated value from my validate function, my jzInput directive does not. How do I manually update the value of the ngModel from within my directive and have it reflected properly?
Example:
Plunker. Type into the top input, both numbers and letters. Note that letters show up, a nice formatted number does not. In the input below, using the same model variable, is showing the correct, expected value. How do I get my jz-input to show the correct, expected value for the model?