diff --git a/bower.json b/bower.json index b85939c..349768b 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "angular-moment-duration", - "version": "0.0.8", + "version": "0.0.9", "homepage": "https://github.com/Recras/angular-moment-duration", "description": "An AngularJS directive for moment.js duration", "main": "./src/angular-moment-duration.js", diff --git a/package.json b/package.json index 3c6e1d7..3444c87 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "angular-moment-duration", - "version": "0.0.8", + "version": "0.0.9", "description": "An AngularJS directive for moment.js duration", "main": "src/angular-moment-duration.js", "scripts": { diff --git a/src/angular-moment-duration.js b/src/angular-moment-duration.js index c809b55..b280e75 100644 --- a/src/angular-moment-duration.js +++ b/src/angular-moment-duration.js @@ -61,18 +61,25 @@ angular.module('ui.moment-duration', []) element.val(Math.floor(value)); }; - ngModel.$viewChangeListeners.push(function() { - var valid = true; - if (attrs.min && parseInt(ngModel.$viewValue, 10) < parseInt(attrs.min, 10)) { - valid = false; - } else if (attrs.max && parseInt(ngModel.$viewValue, 10) > parseInt(attrs.max, 10)) { - valid = false; + var isValid = function(value) { + if (attrs.min && parseInt(value, 10) < parseInt(attrs.min, 10)) { + return false; + } + if (attrs.max && parseInt(value, 10) > parseInt(attrs.max, 10)) { + return false; } - ngModel.$setValidity('min', valid); + return true; + }; + + ngModel.$viewChangeListeners.push(function() { + ngModel.$setValidity('min', isValid(ngModel.$viewValue)); }); ngModel.$parsers.unshift(function(viewValue) { var duration = moment.duration(ngModel.$modelValue); + if (!isValid(viewValue)) { + return duration; + } var newValue; if (scope.maxUnit === undefined) { newValue = duration.get(scope.type);