Created
May 21, 2015 04:12
-
-
Save think2011/a24f2914e955709defda to your computer and use it in GitHub Desktop.
directive 限制输入的最大数,最小数无法设置,因为删除时无法判断
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
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