2016-05-15 4 views
4

Wie blende ich eine Standardfehlermeldung in AngularJs aus? Ich habe versucht Anzeige: keine;. Aber es wird nicht funktionieren. Ich bin neu bei AngularJS. Ich möchte die Standardfehlermeldung ausblenden, und ich möchte die Fehlermeldung anzeigen, wenn Benutzer das Eingabetextfeld onfocus.Standardfehlermeldung in AngularJs ausblenden

<p> 
    <label for="first_name">First Name</label> 
    <input type="text" name="first_name" id="first_name" ng-model="firstName" ng-pattern="/^[a-zA-Z\s]*$/" required/> 
    <span class="error" ng-messages="contact_form.first_name.$error"> 
     <span ng-message="required">First name should not be empty</span> 
     <span ng-message="pattern" ng-show="contact_form.first_name.$error.pattern">Only alphabets allowed</span> 
    </span> 
</p> 
+0

Mögliches Duplikat von https://stackoverflow.com/questions/25634394/how-to-make-ngmessage-for-required-fields-only-show-when-dirty-or-submitting-af/45333650#45333650 – hubatish

Antwort

3

Dies ist, was Sie brauchen, contact_form.first_name.$dirty wenn Feld oder nicht

geändert wurde zu prüfen, verwendet wird
<form name="contact_form" novalidate>  
    <p> 
     <label for="first_name">First Name</label> 
     <input type="text" name="first_name" id="first_name" ng-model="firstName" ng-pattern="/^[a-zA-Z\s]*$/" required/> 
     <span class="error" ng-messages="contact_form.first_name.$error"> 
      <span ng-message="required" ng-show="contact_form.first_name.$error.required && contact_form.first_name.$dirty">First name should not be empty</span> 
      <span ng-message="pattern" ng-show="contact_form.first_name.$error.pattern">Only alphabets allowed</span> 
     </span> 
    </p> 
</form> 
+0

was ist die Verwendung von contact_form.first_name. $ dirty? – Karuppiah

+0

Es wird verwendet, um die Groß-/Kleinschreibung zu überprüfen, wenn die Eingabe interagiert wurde oder nicht https://docs.angularjs.org/guide/forms – gevorg

1

In Ihrem Controller Sie eine Variable erstellen können, um zu bestimmen, ob das Formular sumbitted worden sind:

app.controller('NameController', ['$scope', function($scope) { 
    $scope.submitted = false; 

    $scope.formProcess = function(form) { 
    $scope.submitted = true; 
     // logic 
    } 
}]); 

als in der Ansicht:

<form ng-submit="formProcess(form)"> 
    <p> 
     <label for="first_name">First Name</label> 
     <input type="text" name="first_name" id="first_name" ng-model="firstName" ng-pattern="/^[a-zA-Z\s]*$/" required/> 
     <span class="error" ng-if="submitted" && ng-messages="contact_form.first_name.$error"> 
      <span ng-message="required">First name should not be empty</span> 
      <span ng-message="pattern" ng-show="contact_form.first_name.$error.pattern">Only alphabets allowed</span> 
     </span> 
    </p> 
    <p> 
     <button class="btn btn-secondary" type="submit">Send</button> 
    </p> 
</form>