0

I know you can easily toggle a class like this:

.c {
  padding: 20px;
  background: #00adff;
}

.a {
  background: #43dd31;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="">
  <div class="c" ng-class="{'a': toggle }">
    <button ng-click="toggle = !toggle">Toggle</button>
  </div>
</div>

My question is, what if I have two separate classes I want to toggle with different buttons on the same div using this method?

2
  • 1
    ... what if I have two separate classes I want toggle with different buttons on the same div using this method... I'm sorry, I don't think I understand what you mean. Can you explain a bit more. maybe specify this use case in more detail. Commented Aug 21, 2018 at 14:45
  • @31piy already provided the answer I was looking for and there is hardly a need for further details. Commented Aug 21, 2018 at 15:08

1 Answer 1

2

AngularJS allows you to define numerous classes in the map-format. Following is a demo to toggle a class b using a second button.

.c {
  padding: 20px;
  background: #00adff;
}

.a {
  background: #43dd31;
}

.b {
  border: 5px dashed yellow;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="">
  <div class="c" ng-class="{'a': toggle, b: secondToggle}">
    <button ng-click="toggle = !toggle">Toggle</button>
    <button ng-click="secondToggle = !secondToggle">Second Toggle</button>
  </div>
</div>

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

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.