简体   繁体   English

动态更改ng模式正则表达式

[英]Dynamicaly changing ng-pattern regex

I want to changing regex pattern by same rules. 我想通过相同的规则更改正则表达式模式。 Example is in Plunker 示例在Plunker中

If is selected type, which have regular expression, model is updated, if value is valid. 如果选择了具有正则表达式的类型,则在值有效的情况下更新模型。 But if is selected type, which doesn't have regular expression (eg 'string' in my code), model is never updated. 但是,如果选择的类型没有正则表达式(例如,我的代码中的“字符串”),则模型永远不会更新。 Doesn't matter, if function returned null or empty string . 没关系,如果函数返回null空字符串

My question is, if exist way, how turn this validation off? 我的问题是,如果存在的话,如何关闭此验证?

You can return .* when you want to allow any input: 要允许任何输入,可以返回.*

$scope.getValidatorRegex = function () {
      switch ($scope.type) {
          case 'int':
            return /^\d+$/;
          default:
            return /.*/;
      }
};

You need to wrap your fields in a <form> tag to initiate the angulars built-in validation. 您需要将字段包装在<form>标记中以启动角度内置验证。 See FormController 参见FormController

When you do that, the int validation will work, but since numbers can also be strings, your string validation won't work. 当您这样做时, int验证将起作用,但是由于数字也可以是字符串,因此您的字符串验证将不起作用。

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

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