Use the natural model setter, scope[attrs.ngModel]
instead of setviewvalue.
app.directive('format', function($filter) {
return {
require: 'ngModel',
link: function(scope, element, attrs, ctrl) {
element.unbind('input').unbind('keydown').unbind('change');
element.bind('blur', function() {
if (element.val()) {
scope.$apply(function() {
scope[attrs.ngModel] = element.val();
});
}
});
ctrl.$formatters.unshift(function(modelValue) {
if (modelValue) {
var formatted = $filter('currency')(modelValue);
return formatted;
}
});
}
}
});
You will also need a parser for this to work correctly.