Skip to content

Instantly share code, notes, and snippets.

@mewdriller
Created March 31, 2015 19:36
Show Gist options
  • Save mewdriller/5768d2524bd7983cea4c to your computer and use it in GitHub Desktop.
Save mewdriller/5768d2524bd7983cea4c to your computer and use it in GitHub Desktop.
An AngularJS directive to transclude while using the contextual scope.
'use strict';
module.exports = /*@ngInject*/ function scopedTransclude() {
return {
link: function postLink(scope, iElement, iAttributes, controller, transclude) {
if (!transclude) {
throw minErr('ngTransclude')('orphan',
'Illegal use of ngTransclude directive in the template! ' +
'No parent directive that requires a transclusion found. ' +
'Element: {0}',
startingTag($element));
}
var innerScope = scope.$new();
transclude(innerScope, function (clone) {
iElement.empty();
iElement.append(clone);
iElement.on('$destroy', function () {
innerScope.$destroy();
});
});
}
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment