Created
June 30, 2015 19:13
-
-
Save bbrown/3791ca9d032b437cfd12 to your computer and use it in GitHub Desktop.
Angular directive that toggles a class on an element and insures that no other elements have that 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
| angular.module("hedge").directive("toggleClassSingularly", function() | |
| { | |
| return { | |
| restrict: "A", | |
| link: function(scope, element, attrs) | |
| { | |
| scope.$root.$on("singular-class-toggled", function(event, args) | |
| { | |
| if (args.class === attrs.toggleClassSingularly && args.element !== element) | |
| { | |
| element.removeClass(args.class); | |
| } | |
| }); | |
| element.bind("click", function() | |
| { | |
| element.toggleClass(attrs.toggleClassSingularly); | |
| scope.$root.$broadcast("singular-class-toggled", { class:attrs.toggleClassSingularly, element:element }); | |
| }); | |
| } | |
| }; | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment