Skip to content

Instantly share code, notes, and snippets.

@schleim
Last active November 19, 2015 09:55
Show Gist options
  • Save schleim/de3fc79cbbd0ae6dbd1b to your computer and use it in GitHub Desktop.
Save schleim/de3fc79cbbd0ae6dbd1b to your computer and use it in GitHub Desktop.
Angular directive with expression in attribute
<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>
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