user636525 user636525 - 6 months ago 44
AngularJS Question

Show multiple messages for different form fields using ng-messages

Is it possible to use ng-messages with angular 1.3 and show validation summary on the top with error messages for different fields?

<div ng-messages="myForm.$error">
<div ng-message="required">Need to show required error for field 1.</div>
<div ng-message="required">Need to show required error for field 2.</div>
</div>


How can i achieve this ?
All the examples i have seen so far is showing error messages for one single field, not for all the fields in the form.

Do I need to have multiple ng-messages section for each field?

Answer

I wanted to style the whole summary block as one. So i ended up doing this.

<div class="error-summary" ng-show="myForm.$submitted && myForm.$invalid">
    <p ng-show="myForm.field1.$error.required">Field 1 is required.</p>
    <p ng-show="myForm.field2.$error.required">Field 2 is required.</p>
</div>

I was hoping to do something similar to this using ng-messages.