我正在創建一個directive
以限制輸入框只接受數字。它應該接受1-9999999999,但不是前導0.它正在工作,但輸入第一個數字後,它將接受所有數字和字符。請幫我解決this.it應該接受10,但不是01.因爲我這個代碼,角度指令僅用於輸入數字
HTML:
<input type="text" ng-model="number" required="required" numbers-only="numbers-only" />
JS:
angular.module('myApp', []).directive('numbersOnly', function(){
return {
require: 'ngModel',
link: function(scope, element, attrs, modelCtrl) {
modelCtrl.$parsers.push(function (inputValue) {
if (inputValue == undefined) return ''
var transformedInput = inputValue.replace(/^[^1-9]*/g, '');
if (transformedInput!=inputValue) {
modelCtrl.$setViewValue(transformedInput);
modelCtrl.$render();
}
return transformedInput;
});
}
};
});
function MyCtrl($scope) {
$scope.number = ''
}
FIDDLE:FIDDLE
謝謝。這是工作。 – Ved