Tuesday, 13 October 2015

Toggle class with ng-click on several elements

There is one liner code for Toggle class in jquery
   $('#page-wrapper').toggleClass('nav-small');

But what if we want to do it in a angular way

So I made simple directive for same:

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>



Hey, if there's anybody I can help out there, just let me know. Thanks ;)

No comments:

Post a Comment