错误验证在AngularJS中不起作用

Error validation not working in AngularJS

本文关键字:不起作用 AngularJS 验证 错误      更新时间:2024-01-05

我正在Angular中进行错误验证,如下所示。

<select ng-model = "$parent.color" class = "form-control"
    ng-options = "color as color for color in $parent.colors" required>
    <option value="">Choose an option</option> 
</select>
<span ng-show="serviceForm.$parent.color.$error.required"> My custom error message </span>

错误验证消息永远不会显示。如何解决此问题?

是否包含ngRequire模块?

ng需要

<script>
angular.module('ngRequiredExample', [])
.controller('ExampleController', ['$scope', function($scope) {
  $scope.required = true;
}]);
</script>
<div ng-controller="ExampleController">
 <form name="form">
 <label for="required">Toggle required: </label>
 <input type="checkbox" ng-model="required" id="required" />
 <br>
 <label for="input">This input must be filled if `required` is true:</label>
 <input type="text" ng-model="model" id="input" name="input" ng-required="required" /> <br>
 <hr>
 required error set? = <code>{{form.input.$error.required}}</code><br>
 model = <code>{{model}}</code>

如果有效的话,试试这个。

您的代码可以像这样正常工作。

<form name="form1">
   <select name="select" ng-model = "$parent.color" class = "form-control"
       ng-options = "color as color for color in $parent.colors" required>
       <option value="">Choose an option</option>    
   </select>
   <span ng-show="form1.select.$error.required"> My custom error message </span>
</form>

您没有流动FormController。查看此链接