I am trying to create a directive that formats a decimal value to a time value h:mm. If a user enters a time value the model should be updated with the decimal representation. If a user enters a decimal value his input should be replaced with the formatted value.
I am using the directive as follows:
<input type="text" hour-input ng-model="vm.hours"/>
Here is the relevant code:
app.directive('hourInput', hourInput);
function hourInput() {
var directive = {
link: link,
restrict: 'A',
require: 'ngModel'
};
return directive;
function link(scope, element, attrs, ngModelController) {
// model -> view
ngModelController.$formatters.push(function (value) {
return formatTime(value);
});
// view -> model
ngModelController.$parsers.push(function (value) {
var result;
if (!/^\d?[\d,\.]*$/.test(value)) {
result = parseTime(value);
} else {
result = parseFloat(value);
}
return result;
});
}
}
function parseTime(value) {
// code removed for brevity
return hours + minutes / 60;
}
function formatTime(value) {
// code removed for brevity
return result;
}
Here is the plunker. The interaction with the model is working. However the formatted time is not updated in the UI.
$applygo?