簡體   English   中英

錯誤驗證在AngularJS中不起作用

[英]Error validation not working in AngularJS

我在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模塊?

ngRequire

<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。 查看此鏈接

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM