Created
March 8, 2016 05:59
-
-
Save shamsher31/876590ecddb098c49e8f to your computer and use it in GitHub Desktop.
Angular Toggel Class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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