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?

drewwyatt
  • 5,989
  • 15
  • 60
  • 106
  • 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. – sethetter Apr 01 '14 at 15:09

5 Answers5

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.

srdan
  • 546
  • 4
  • 7
3

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

sethetter
  • 576
  • 5
  • 11
0

I guess you can use angularjs ng-style

Tiago Moutinho
  • 1,372
  • 1
  • 13
  • 18
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')";
Community
  • 1
  • 1
alphapilgrim
  • 3,761
  • 8
  • 29
  • 58
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>
Ben Besuijen
  • 624
  • 9
  • 23