Skip to content

Instantly share code, notes, and snippets.

@icholy
Created November 4, 2013 15:54
Show Gist options
  • Save icholy/7304674 to your computer and use it in GitHub Desktop.
Save icholy/7304674 to your computer and use it in GitHub Desktop.
Duration.js + Angular.js Directive
<input type="text" ng-model="myDuration" duration>
angular.module('App').directive('duration', function () {
return {
retrict: 'A',
require: 'ngModel',
link: function (scope, element, attr, ngModel) {
ngModel.$parsers.push(function (str) {
try {
var d = Duration.parse(str);
ngModel.$setValidity('durationValidate', true);
return d.valueOf();
} catch (e) {
ngModel.$setValidity('durationValidate', false);
return undefined;
}
});
ngModel.$formatters.push(function (ms) {
return new Duration(ms).toString();
});
}
};
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment