Toggle class with ng-click on several elements
I made simple directive for testing:
module.directive('toggleClass', function() {
return {
restrict: 'A',
link: function(scope, element, attrs) {
element.bind('click', function() {
element.toggleClass(attrs.toggleClass);
});
}
};
});
so you can make any element toggle class you need
<button id="btn" toggle-class="active">Change Class</button>
<div toggle-class="whatever"></div>
inside controller
$scope.stateToggle=false;
$scope.togglelasses=function(){
$scope.stateToggle= !$scope.stateToggle;
}
in view use ng-class
<button ng-click="togglelasses()" ng-class="stateToggle? 'active ': ' '">
add ng-class in every element where you want class active
Depending on your requirements, you may be able to use an ng-repeat
with an array representing the toggles. For example:
Your view:
<div ng-repeat="toggle in toggles">
<button id="btn" ng-click="toggle.state = !toggle.state" ng-class="{'red' : toggle.state}">Change Class</button>
</div>
Inside your controller:
$scope.toggles = [{ state: true }, { state: false }, { state: true }];
This way you can expand on your button set by simply updating the array, or the internal array objects (should you need more complexity).