3
<div id="parent" ng-class="{testClass:???}">
   <span id="child" class="test"/>
</div>

On this example, how would I do it so that if element child would have a class test, the parent element would dynamically have the class testClass?

Shashank Agrawal
  • 25,161
  • 11
  • 89
  • 121
Albert Cruz
  • 31
  • 1
  • 2
  • Do you control the class of the child? I mean its obviously dynamic in some form but if its not set with `ng-class` how is it set? – ste2425 Dec 15 '15 at 08:15
  • Possible duplicate of [AngularJS ngClass conditional](http://stackoverflow.com/questions/16529825/angularjs-ngclass-conditional) –  Dec 15 '15 at 08:16

2 Answers2

3

You can create a scope variable to validate whether those 2 elements should be visible or not. The inner span too should be getting set dynamically according to your description. So you can use ng-class for that too. So the code can be like this :

<div id="parent" ng-class="{testClass : isValid}">
    <span id="child" ng-class="{test : isValid}"/>
</div>
Adarsh Konchady
  • 2,577
  • 5
  • 30
  • 50
2

You can add the ternary condition within the ng-class like

var var-test = 'hi'=='bye'
ng-class=" var-test ? 'class-if-true' : 'class-if-false'">

and put in your css file the classes

.class-if-true{color=black}
.class-if-false{color=white}
Franz Kurt
  • 1,020
  • 2
  • 14
  • 14