$parse:ueoe 在解析指令中绑定的 ng-pattern 时在 angular js 中出错

$parse:ueoe Error in angular js when parsing ng-pattern bound in directive

我有一个指令绑定了一个字符串以在指令本身中作为 ng-pattern 应用,但是在呈现时它给出了 UEOE 错误。

指令定义为:

myApp.directive('myDirective', Directive);

function Directive() {
    var directiveDefinition = {
        restrict: 'E',
        require: 'ngModel',
        replace: true,
        transclude: false,
        link: function (scope, element, attrs, ngModelCtrl) {          

        },
        scope: { 
            myPattern:'@',
           },

        templateUrl: 'template.htm'
    };
    return directiveDefinition;
} 

有模板

<div>
   <form  name=myForm>
        <input type='text' ng-model='ngModel'
               ng-model-options="{ updateOn:'blur', allowInvalid: true }" 
               ng-pattern="{{myPattern}}" /> 
    </form>        
</div>

它在视图中用作:

<my-directive ng-model="someValue" my-pattern="[a-z]+"></my-directive>

报错

angular.min.js:2 Error: [$parse:ueoe] http://errors.angularjs.org/1.6.4/$parse/ueoe?p0=%5Ba-z%5D%2B

如果我将正则表达式更改为没有 'special' 个字符,那很好,但是对于任何具有复杂正则表达式的东西,它都不起作用,例如,我得到了正则表达式 [A-Za-z0-9] 的 lexerr .]+

http://errors.angularjs.org/1.6.4/$parse/lexerr?p0=Unexpected%20next%20character%20&p1=s%2010-10%20%5B%5C%5D&p2=%5BA-Za-z0-9%5C.%5D%2B

有什么方法可以将这样的正则表达式传递给指令,以便它可以成功解析吗?

顺便说一句,ng-pattern 工作正常,但我发现其他意外行为我怀疑是连锁效应。[​​=16=]

从文档来看,似乎 ng-patterns 期望 angular 表达式,而不是大多数情况下的插值 ex ng-pattern="myPattern"。没有测试我自己,如果使用插值而不是最终结果必须是正则表达式文字形式 ex。 /abc/

来自the api docs

the value is an AngularJS expression: If the expression evaluates to a RegExp object, then this is used directly.

If the expression evaluates to a string, then it will be converted to a RegExp after wrapping it in ^ and $ characters. For instance, "abc" will be converted to new RegExp('^abc$').

If the value is a RegExp literal, e.g. ngPattern="/^\d+$/", it is used directly.

ngPattern

AngularJS expression that must evaluate to a RegExp or a String parsable into a RegExp, or a RegExp literal. See above for more details.