Skip to content

Instantly share code, notes, and snippets.

@think2011
Created May 21, 2015 04:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save think2011/a24f2914e955709defda to your computer and use it in GitHub Desktop.
Save think2011/a24f2914e955709defda to your computer and use it in GitHub Desktop.
directive 限制输入的最大数,最小数无法设置,因为删除时无法判断
var app = angular.module('limit-max', []);
/**
* 限制输入的最大数,最小数无法设置,因为删除时无法判断
* @example
* <input ng-model="xx" type="number" limit-max="100">
*/
app.directive('limitMax', function() {
return {
restrict : 'A',
scope: {
ngModel: '='
},
link: function(scope, element, attrs) {
var max = +attrs.limitMax;
scope.$watch('ngModel', function(newVal, oldVal) {
var isNumber = /^\d{1,}$/.test(newVal);
if(!isNumber) {
return scope.ngModel = null;
}
if(max && newVal > max) scope.ngModel = max;
});
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment