Skip to content

Instantly share code, notes, and snippets.

@shamsher31
Created March 8, 2016 05:59
Show Gist options
  • Save shamsher31/876590ecddb098c49e8f to your computer and use it in GitHub Desktop.
Save shamsher31/876590ecddb098c49e8f to your computer and use it in GitHub Desktop.
Angular Toggel Class
// Ref: http://stackoverflow.com/questions/25141139/toggle-class-with-ng-click-on-several-elements
module.directive('toggleClass', function() {
return {
restrict: 'A',
link: function(scope, element, attrs) {
element.bind('click', function() {
element.toggleClass(attrs.toggleClass);
});
}
};
});
<button id="btn" toggle-class="active">Change Class</button>
<div toggle-class="whatever"></div>
// Ref http://stackoverflow.com/questions/23900147/angularjs-directive-to-add-class-on-click-but-remove-and-add-it-to-another-elem
.directive('swapit', function(){
return {
restrict: 'E',
replace: true,
transclude: true,
scope: {
active: '='
},
template: '<a ng-click="active = $id" ng-class="{active: $id === active}" ng-transclude></a>'
}
})
<swapit active="active">Still got game</swapit>
<swapit active="active">TnT</swapit>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment