【问题标题】:AngularJS bind ng-valid with functionAngularJS 将 ng-valid 与函数绑定
【发布时间】:2018-10-15 21:01:14
【问题描述】:

您好,我想使用表达式设置输入的有效性,但不是正则表达式,像这样

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

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

【问题讨论】:

标签: angularjs twitter-bootstrap angularjs-forms


【解决方案1】:

你可以试试这样的:

<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;
          }
        }
      }  


    });

})();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-28
    • 2017-11-24
    • 2016-04-19
    • 2013-03-13
    • 2014-10-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多