Last active
November 19, 2015 09:55
-
-
Save schleim/de3fc79cbbd0ae6dbd1b to your computer and use it in GitHub Desktop.
Angular directive with expression in attribute
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
| <section class="accordion" accordion accordion-on-open="showImage=true" > | |
| <dl> | |
| <dt> | |
| <a href="" class="">Link</a> | |
| </dt> | |
| <dd class="cf "> | |
| Content | |
| <img ng-if="showImage" src=""> | |
| </dd> | |
| </dl> |
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
| myAngularApp.directive('accordion', | |
| function() { | |
| return { | |
| restrict: 'AE', | |
| scope:{ | |
| accordionOnOpen: '&' // write attribute to scope variable | |
| }, | |
| link: function(scope, element, attrs, controller) { | |
| var $link = $(element).find('dt a'); | |
| var $pane = $(element).find('dd'); | |
| $(element).off('click').on('click',function(){ | |
| if(!$link.hasClass('active')){ | |
| $pane.slideDown(); | |
| $link.addClass('active'); | |
| scope.$apply(function() { | |
| scope.$eval(scope.accordionOnOpen); // compile attribute expression | |
| }); | |
| }else{ | |
| $pane.slideUp(); | |
| $link.removeClass('active'); | |
| } | |
| }); | |
| } | |
| }; | |
| } | |
| ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment