2016-04-05 5 views
1

Ich mache Fehler Validierung in Angular wie folgt.Fehler-Validierung funktioniert nicht in AngularJS

<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> 

Die Fehlerüberprüfungsmeldung wird nie angezeigt. Wie behebe ich das?

Antwort

2

Haben Sie das ngRequire-Modul hinzugefügt?

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> 

dies versuchen, wenn es funktioniert.

0

Ihr Code kann so gut funktionieren.

<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> 

Sie sind nicht fließenden FormController. View this link

+0

Gibt es eine Möglichkeit, die Nachricht nur bei submit zu zeigen? Gerade jetzt zeigt es immer –

+0

Ja können Sie eine Scope-Variable als eine Flagge von submit verwenden, heben Sie es auf, wenn submit aufgerufen wird, sonst machen Sie es falsch. und benutze es auch in ng-show-Bedingung mit && operator. Ex $ scope.submitted – rahulgarg