为什么不与下面的正则表达式的工作Angularjs NG-格局? [英] Why is Angularjs ng-pattern not working with the following regexp?
问题描述
有关某些原因初始化值未出现在现场,但没有纳克图案的第二场确实工作。任何想法?
For some reason the initialized value doesn't appear in the field, but the second field without the ng-pattern does work. any ideas?
angular.module('app', []).controller('MainCtrl', function($scope) {
$scope.widget = {title: 'abc', title2: 'abc'};
});
<div ng-app="app" ng-controller="MainCtrl">
<input ng-model="widget.title" required ng-pattern="/[a-zA-Z]{4}[0-9]{6,6}[a-zA-Z0-9]{3}/">
<br /><br />
input 1: {{ widget.title }}
<br /><br />
<input ng-model="widget.title2" required>
<br /><br />
input 2: {{ widget.title2 }}
</div>
下面是小提琴
http://jsfiddle.net/wkzab/1/
推荐答案
更新
我看着它一点(从来没有真正使用角),并通过使用的形式和投入上的名字
属性,你可以抓住一个错误,如显示在我的最新的jsfiddle 。其格式为:{{$ formName.inputName错误}}
。这将返回与等于一个布尔值参数的对象。因此, {{form.title。$ error.pattern}}
将真当是普通的错误
前pression(所以你会显示错误)。我也打扫(的工作原理相同)的正则表达式为: / ^ [AZ] {4} \\ d {6} [AZ \\ D] {3} $ / I
。
I looked into it a bit (never really used Angular), and by using the name
attribute on the form and inputs, you can grab an error like shown in my newest JSFiddle. Its in the format: {{formName.inputName.$error}}
. This returns an object with parameters that equal a boolean. So {{form.title.$error.pattern}}
will be true
when there is an error with the regular expression (so you would display the error). I also cleaned (works the same) your regex to: /^[A-Z]{4}\d{6}[A-Z\d]{3}$/i
.
OLD
的 NG-模式
属性试图在此基础上定期EX pression字段中匹配:的 / [A-ZA-Z] {4} [0-9] {6,6} [A-ZA-Z0-9] {3} /
。这相当于4字母字符,6位,和3个字母数字字符。一旦你有一个匹配的模式,它会显示出来。
The ng-pattern
attribute attempts to match the field based on this regular expression: /[a-zA-Z]{4}[0-9]{6,6}[a-zA-Z0-9]{3}/
. This translates to 4 alphabetical characters, 6 digits, and 3 alphanumeric characters. Once you have a matching pattern, it will show up.
您应该能够删除 NG-模式
属性或者更改前pression不那么具体。例如,这的jsfiddle 只要整个字符串是字母数字将接受任何价值。如果你想用不同的方式帮助更新的问题。
You should be able to remove the ng-pattern
attribute or change the expression to be less specific. For example, this JSFiddle will accept any value as long as the entire string is alphanumeric. Update the question if you want help with a different pattern.
这篇关于为什么不与下面的正则表达式的工作Angularjs NG-格局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!