Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angularjs: ng-message always showing

Tags:

I'm using angular-messages to display form validation errors on my angular app. As per the documentation, I have built the following code

<form name="loginForm">   <label class="item item-input">     <input type="email" placeholder="Email" ng-model="data.email" name="email" required>   </label>   <div ng-messages="loginForm.email.$error" style="color:maroon">     <div ng-message="required">Please input a valid e-mail address</div>     <div ng-message="email">You did not enter your email address correctly...</div>   </div> </form> 

I have included the ngMessages directive in my javascript as well as imported the angular-messages.js file.

Unfortunately, these two messages are showing perpetually. Regardless of what I type in the input field, be it a valid email or not. Both messages are always showing. If I try to only include one ng-message, the result is the same.

What could I be doing wrong?

edit: In case my description isn't very clear, this is a print of the result https://s9.postimg.cc/du9230tdb/Screen_Shot_2015_06_26_at_17_09_24.png

like image 414
Paraphiliac Ostrich Avatar asked Jun 25 '15 17:06

Paraphiliac Ostrich


2 Answers

You gotta make sure you are actually including ngMessage to your module.

var app = angular.module('app', [     'ngMessages' ]) 

... and that you included the library to your project

<script src="/scripts/vendors/angular-messages/angular-messages.js"></script> 
like image 174
Carlos A. Cabrera Avatar answered Sep 28 '22 15:09

Carlos A. Cabrera


Everything seems to be fine in the code you're sharing.

<form name="loginForm">     <label class="item item-input">         <input type="email" placeholder="Email" ng-model="data.email" name="email" required>     </label>     <div ng-messages="loginForm.email.$error" style="color:maroon">         <div ng-message="required">Please input a valid e-mail address</div>         <div ng-message="email">You did not enter your email address correctly...</div>     </div> </form> 

Here is a working copy on Plunker I'm using your piece of code.

From Angularjs documentation.

By default, ngMessages will only display one error at a time. However, if you wish to display all messages then the ng-messages-multiple attribute flag can be used on the element containing the ngMessages directive to make this happen.

If you want to show the errors after the field is dirty, please visit this link.

Make sure you are including ngMessage module and the library as well. Please see Carlos's answer.

Thanks

like image 20
jlulloav Avatar answered Sep 28 '22 14:09

jlulloav