4

My form elements look like this:

<div class="control-group">
    <label for="email">Email Address</label>
    <input type="email" class="form-control" name="email" ng-model="message.emailAddress" id="email" />
</div>

Angular automatically adds the class "ng-invalid" to the input when the email address is invalid - but I would also like it to add a class the label or the control-group.

Is that possible? or is there an easy workaround?

1
  • 3
    Adding this functionality yourself would require the use of a directive. You could maybe put an attribute directive on the control-group that detects if the input elemnt has the ng-invalid class and apply it to the label element as well if so. Commented Apr 1, 2014 at 15:09

5 Answers 5

5

If your form's name is myForm, you could add

ng-class="{'some-class-name': !myForm.email.$valid}"

to the label and/or control group element.

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

Comments

3

Classes are added to the parent form element labeled as ng-invalid-inputName. You could style off of this.

Comments

0

I guess you can use angularjs ng-style

Comments

0

You could do:

ng-class="mode.input.$valid ? 'someclas': 'otherclass'";

or nested for multiple class or checks: as per: https://stackoverflow.com/a/21970785/4812515stack post

ng-class="model.input.$valid ? 'someclas': (model.input.$valid ? 'class':'class')";

Comments

0

If you add an error class to .control-group you can style the parent of the input, but also the label and input.

Just check the boolean yourForm.email.$invalid with ng-class. yourForm is the name attribute value of the form element and email the name attribute vale of the input element.

<form name="yourForm">
    <div class="control-group" ng-class="{'control-group-error': yourForm.email.$invalid}">
        <label for="email">Email Address</label>
        <input type="email" class="form-control" name="email" ng-model="message.emailAddress" />
    </div>    
</form>

Comments

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.