Created
January 31, 2016 13:05
-
-
Save djvs/bf393cf5d53ec1e27b54 to your computer and use it in GitHub Desktop.
Angular force number with range on input[type='text']
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.directive('forcenum', function ($parse) { | |
return { | |
restrict: 'A', | |
require: 'ngModel', | |
link: function link(scope, element, attr, ngModel) { | |
var max = parseInt(attr.fmax); | |
var min = parseInt(attr.fmin); | |
var nv; | |
scope.$watch(function () { | |
return ngModel.$modelValue; | |
}, function (val) { | |
var value = parseInt(val); | |
if (value > max) { | |
nv = max; | |
} else if (value < min) { | |
nv = min; | |
} else if (!value) { | |
return; | |
} else { | |
nv = value; | |
} | |
log(value, nv); | |
ngModel.$setViewValue(nv); | |
element.val(nv); | |
}); | |
} | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment