简体   繁体   English

角度js中的条件ng-模式

[英]Conditional ng-pattern in angular js

Is there a way to achieve conditional ng-pattern in angularjs 有没有办法在angularjs中实现条件ng-pattern

ng-pattern="(myForm.exipration.$dirty ? ('/^\d{2}[/]\d{4}$/') : '')"

i tried like above, but does not help. 我尝试过如上所述,但没有帮助。

Markup 标记

<input ... ng-pattern="handlePatternPassword">

Controller 调节器

$scope.handlePatternPassword = (function() {
  var regex = /^[A-Za-z0-9!@#$%^&*()_]{4,20}$/;
  return {
    test: function(value) {
      if ($scope.user.isLogged) {
        return (value.length > 0) ? regex.test(value) : true;
      } else {
        return regex.test(value);
      }
    }
  };
})();

From https://stackoverflow.com/a/18984874/4640499 . 来自https://stackoverflow.com/a/18984874/4640499

以下对我有用(AngularJS v1.5.11):

ng-pattern="condition ? '[A-Za-z0-9_-]{1,60}' : ''"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM