AngularJS 1.3模式验证绑定不工作

问题描述:

我一直在使用正则表达式验证与AngularJS在过去的几个版本,它一直很好。

I have been using Regex pattern validation with AngularJS for the last several versions and it has worked fine.

我的应用程序需要的验证图案由一个范围属性公开,向其中相应AngularJS验证指令的约束。到V1.3之前,它看起来是这样的:

My application requires that validation patterns are exposed by a scope property, to which the corresponding AngularJS validation directive is bound. Prior to v1.3, it looked something like this:

// On the controller
$scope.validationPattern = "^\\d*$"; // Allow only numeric digits

<!-- in the HTML page --->
<input type="text" name="age" ng-pattern="/{{validationPattern}}/" />

现在有更新AngularJS到V1.4(绕过V1.3),我觉得上面的方法不再起作用。看着为V1.3 的迁移笔记,我看到,这是正常现象,一个新的方法是必需的,这看起来是这样的:

Having now updated AngularJS to v1.4 (bypassing v1.3), I find that the above approach no longer works. Looking at the migration notes for v1.3, I see that this is expected behavior and that a new approach is required, which looks something like this:

// On the controller
$scope.validationRegexp = /^\d*$/; // Use a RegExp instead of a string

<!-- in the HTML page --->
<input type="text" name="age" pattern="{{validationRegexp}}" />

不过,我根本无法得到这个工作。如果我把验证模式内嵌(HTML输入元素中),它工作正常,但是当移动到的范围对象,并绑定到模式 NG-模式指令时,没有验证。

这里有一个的jsfiddle 演示该问题。

有什么建议吗?

您应该使用范围变量只有名称:

You should use only the name of the scope variable:

<input type="text" name="age" ng-pattern="validationPattern" />