我有脚本here和ng-pattern正常工作,因为scope.subnet只有在输入匹配模式后才会显示在输出中。但是如果ng-pattern不匹配,ng-show不会显示任何错误
<body ng-contoller="myConfigGenCtr">
<form novalidate name="myForm">
<div class="form-group">
<label for="hostname">Firewall hostname</label>
<input type="text" ng-model="hostname" class="form-control" id="hostname">
</div>
<div class="form-group">
<label for="subnet">Firewall subnet</label>
<input type="text" ng-model="subnet" class="form-control" id="subnet"
required ng-pattern="/^(?:[0-9]{1,3}\.){3}/" >
<div class="custom-error" ng-show="myForm.subnet.$error.pattern">
Not a valid subnet, should be i.e. 10.x.y. (3 bytes only)</div>
</div>
</form>
<div>Output: {{subnet}}</div>
</body>发布于 2015-08-14 02:49:18
当您添加带有其名称的表单标记时,angular会为该name属性值创建一个scope变量&并添加表单中具有name属性的所有表单字段。这些字段属性变量在表单作用域对象中创建。与这里一样,您使用的是myForm,这意味着$scope.myFrom拥有有关表单域的所有信息。比如使用$valid、$invalid、$error等来验证它的有效性。
在这里,您在表单的subnet元素上使用ng-show="myForm.subnet.$error.pattern"。您错过了在输入字段上添加name="subnet"属性,这导致subnet元素验证在myForm范围变量中不可用。
标记
<input type="text" name="subnet" ng-model="subnet" class="form-control"
id="subnet" required ng-pattern="/^(?:[0-9]{1,3}\.){3}/" >https://stackoverflow.com/questions/31995684
复制相似问题