Hide default error message in AngularJs

How to hide default error message in AngularJs? I tried display: none; . But that will not work. I am new to AngularJS. I want to hide the default error message, and I want to show the error message when the user inserts focus into the input text box.

<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>
+4
source share
2 answers

This is what you need to contact_form.first_name.$dirtyuse to check if the field has been changed or not.

<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>
+3
source

In the controller, you can create a variable to determine if the form has been deleted:

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

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

Than in your opinion:

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

All Articles