AngularJS与函数绑定ng-valid



嗨,我想使用表达式而不是正则表达式设置输入的有效性,如下所示

<input ng-model="number" required ng-valid="number / 2 > 14">

是否有可能我有复杂的功能,我不知道如何将其转换为正则表达式,所以我想使用这样的东西。我尝试使用这个库:https://github.com/turinggroup/angular-validator 但它根本不起作用:/

你可以尝试这样的事情:

<div ng-app="myApp">
<div ng-controller="myController">
<input ng-model="number" required ng-keyup="checkValid(number)">
<span style="color:red" ng-if="!isValid">Not a valid number</span>
</div>
</div>

并创建一个如下所示的函数:

(function(){
var myApp = angular.module('myApp', []);
myApp.controller('myController', function($scope, $timeout){
$scope.isValid = true;
$scope.checkValid = function(value){
if(value){
if(value/2>14){
$scope.isValid = true;
}else{
$scope.isValid = false;
}
}
}  

});
})();

最新更新