2

From here > Angular ngMessages change class when valid and not only on error

I've tried this

<div ng-messages="userForm.name">
    <i ng-message="$error.required" class="fa fa-exclamation-circle"></i>
    <i ng-message="$valid" class="fa fa-exclamation-check"></i>
</div>

this

<div ng-messages="userForm.name.$error">
    <i ng-message="required" class="fa fa-exclamation-circle"></i>
    <i ng-message="$valid" class="fa fa-exclamation-check"></i>
</div>

and this

<div ng-messages="userForm.name.$error">
    <i ng-message="$error" class="fa fa-exclamation-circle"></i>
    <i ng-message="userForm.name.$valid" class="fa fa-exclamation-check"></i>
</div>

And a few more that I won't bother to list. None of them worked. Can I get a working example please

0

1 Answer 1

2

ngMessages is checking list of particular type, for example if $error exists then it is checking for list of types of errors, like required, min-length, max-length etc.

In your case you are trying to use, $valid inside $error. It cannot happen because if it is valid, then it will not have an error.

I think you can simply use ng-show attribute to display messages like this

<div ng-show='myForm.myName.$error.required'>required</div>
<div ng-show='myForm.myName.$valid'>valid</div>

or In your case

<i ng-show='myForm.myName.$error.required' class='fa fa-exclamation-circle'></i>
<i ng-show='myForm.myName.$valid' class='fa fa-exclamation-check'></i>

see this example http://plnkr.co/edit/z1Lrz17z9qnViq9HAahg?p=preview

Sign up to request clarification or add additional context in comments.

2 Comments

This is definitely what I want, gonna try it now
...and it works, and with less syntax fluff than ngMessages

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.