有没有办法防止在元素中选择非正数/负数?
答案 0 :(得分:0)
试试这个 http://jsfiddle.net/8UBLR/
var $scope;
var app = angular.module('myapp', []);
app.controller('Ctrl', function($scope) {
$scope.wks = {number: 1, validity: true}
});
app.directive('isNumber', function () {
return {
require: 'ngModel',
link: function (scope) {
scope.$watch('wks.number', function(newValue,oldValue) {
var arr = String(newValue).split("");
if (arr.length === 0) return;
if (isNaN(newValue)) {
scope.wks.number = oldValue;
}
});
}
};
});
<form ng-app="myapp" name="myform" novalidate>
<div ng-controller="Ctrl">
<input name="number" is-number ng-model="wks.number">
<span ng-show="!wks.validity">Value is invalid</span>
</div>
</form>
答案 1 :(得分:-3)
您可以使用正则表达式指定您希望用户输入的输入类型。