Skip to content
This repository has been archived by the owner on Feb 23, 2023. It is now read-only.

Update ngTranscludeMod.js to be angular 1.5 compatible #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,9 @@ ngTranscludeMod
==============

A modification adding features to Angular's ngTransclude

This branch allows for angular 1.5 multi-slot-transclusion

Here is a plunker displaying all the different types of transclusion options

http://plnkr.co/edit/5XGBEX0muH9CSijMfWsH?p=preview
105 changes: 63 additions & 42 deletions ngTranscludeMod.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,72 @@
angular.module( 'ngTranscludeMod', [] )
angular.module('ngTranscludeMod', [])
.config([
'$provide', function ($provide) {
$provide.decorator('ngTranscludeDirective', [
'$delegate', function ($delegate) {
// Remove the original directive
$delegate.shift();
return $delegate;
}
]);
}
])
.directive('ngTransclude', function () {
return {
restrict: 'EAC',
replace: true,
link: function ($scope, $element, $attrs, controller, $transclude) {

.config(['$provide', function($provide) {
$provide.decorator('ngTranscludeDirective', ['$delegate', function($delegate) {
// Remove the original directive
$delegate.shift();
return $delegate;
}]);
}])
var types = ['child', 'parent', 'sibling'];
var iScopeType = types[types.indexOf($attrs.ngTransclude)] || undefined;

.directive( 'ngTransclude', function() {
return {
restrict: 'EAC',
link: function( $scope, $element, $attrs, controller, $transclude ) {
if (!$transclude) {
throw minErr('ngTransclude')('orphan',
if ($attrs.ngTransclude === $attrs.$attr.ngTransclude || iScopeType) {
// If the attribute is of the form: `ng-transclude="ng-transclude"`
// then treat it like the default
$attrs.ngTransclude = '';
}

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) );
}
startingTag($element));
}

var iScopeType = $attrs['ngTransclude'] || 'sibling';

switch ( iScopeType ) {
case 'sibling':
$transclude( function( clone ) {
$element.empty();
$element.append( clone );
});
break;
case 'parent':
$transclude( $scope, function( clone ) {
$element.empty();
$element.append( clone );
});
break;
case 'child':
var iChildScope = $scope.$new();
$transclude( iChildScope, function( clone ) {
$element.empty();
$element.append( clone );
$element.on( '$destroy', function() {
iChildScope.$destroy();
});
});
break;
//default function for transclude (same as 'sibling')
var ngTranscludeScope;
// If there is no slot name defined or the slot name is not optional
// then transclude the slot
var ngTranscludeSlotName = $attrs.ngTransclude || $attrs.ngTranscludeSlot;
var ngTranscludeCloneAttachFn = function (clone) {
if (clone.length) {
$element.empty();
$element.append(clone);
}
};

switch (iScopeType) {
case 'parent':
ngTranscludeScope = $scope;
break;
case 'child':
ngTranscludeScope = $scope.$new();
ngTranscludeCloneAttachFn = function (clone) {
$element.empty();
$element.append(clone);
$element.on('$destroy', function () {
ngTranscludeScope.$destroy();
});
};
break;
}
//Scope cant be null
if (ngTranscludeScope) {
$transclude(ngTranscludeScope, ngTranscludeCloneAttachFn, null, ngTranscludeSlotName);
} else {
$transclude(ngTranscludeCloneAttachFn, null, ngTranscludeSlotName);
}
}
}
});
};
});