AngularJS 1.3 pattern validation binding not working

﹥>﹥吖頭↗ 提交于 2019-12-12 05:00:00

问题


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

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}}/" />

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}}" />

However, I simply can't get this to work. If I place the validation pattern inline (within the HTML input element) it works fine, but when moved onto the scope object and bound to the pattern or ng-pattern directive, no validation occurs.

Here's a JSFiddle that demonstrates the problem.

Any suggestions please?


回答1:


You should use only the name of the scope variable:

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


来源:https://stackoverflow.com/questions/31323111/angularjs-1-3-pattern-validation-binding-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!