Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit fe4d16b

Browse files
feat($compile): multiple transclusion via named slots
1 parent 049d3de commit fe4d16b

File tree

3 files changed

+420
-9
lines changed

3 files changed

+420
-9
lines changed

src/ng/compile.js

+65-2
Original file line numberDiff line numberDiff line change
@@ -1486,6 +1486,13 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
14861486
});
14871487
};
14881488

1489+
// We need to attach the transclusion slots onto the `boundTranscludeFn`
1490+
// so that they are available inside the `controllersBoundTransclude` function
1491+
var boundSlots = boundTranscludeFn.$$slots = createMap();
1492+
for (var slotName in transcludeFn.$$slots) {
1493+
boundSlots[slotName] = createBoundTranscludeFn(scope, transcludeFn.$$slots[slotName], previousBoundTranscludeFn);
1494+
}
1495+
14891496
return boundTranscludeFn;
14901497
}
14911498

@@ -1826,6 +1833,52 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
18261833
// on the same element more than once.
18271834
nonTlbTranscludeDirective: nonTlbTranscludeDirective
18281835
});
1836+
} else if (isObject(directiveValue)) {
1837+
1838+
// We have multiple transclusion zones - match them and collect them up into slots
1839+
$template = [];
1840+
var slots = createMap();
1841+
var slotNames = createMap();
1842+
var filledSlots = createMap();
1843+
1844+
// Parse the slot names: if they start with a ? then they are optional
1845+
forEach(directiveValue, function(slotName, key) {
1846+
var optional = (slotName.charAt(0) === '?');
1847+
slotName = optional ? slotName.substring(1) : slotName;
1848+
slotNames[key] = slotName;
1849+
slots[slotName] = [];
1850+
// filledSlots will contain true for all slots that are either
1851+
// optional or have been filled
1852+
filledSlots[slotName] = optional;
1853+
});
1854+
1855+
1856+
forEach($compileNode.children(), function(node) {
1857+
var slotName = slotNames[directiveNormalize(nodeName_(node))];
1858+
var slot = $template;
1859+
if (slotName) {
1860+
filledSlots[slotName] = true;
1861+
slots[slotName].push(node);
1862+
} else {
1863+
$template.push(node);
1864+
}
1865+
});
1866+
1867+
// Check for required slots that were not filled
1868+
forEach(filledSlots, function(filled, slotName) {
1869+
if (!filled) {
1870+
throw $compileMinErr('reqslot', 'Required transclusion slot `{0}` was not filled.', slotName);
1871+
}
1872+
});
1873+
1874+
// Compile each slot into a transclusion function and attach them to the default transclusion function
1875+
childTranscludeFn = compilationGenerator(mightHaveMultipleTransclusionError, $template, transcludeFn);
1876+
forEach(Object.keys(slots), function(slotName) {
1877+
slots[slotName] = compilationGenerator(mightHaveMultipleTransclusionError, slots[slotName], transcludeFn);
1878+
});
1879+
childTranscludeFn.$$slots = slots;
1880+
$compileNode.empty(); // clear contents
1881+
18291882
} else {
18301883
$template = jqLite(jqLiteClone(compileNode)).contents();
18311884
$compileNode.empty(); // clear contents
@@ -2134,11 +2187,11 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
21342187

21352188
// This is the function that is injected as `$transclude`.
21362189
// Note: all arguments are optional!
2137-
function controllersBoundTransclude(scope, cloneAttachFn, futureParentElement) {
2190+
function controllersBoundTransclude(scope, cloneAttachFn, futureParentElement, slotName) {
21382191
var transcludeControllers;
2139-
21402192
// No scope passed in:
21412193
if (!isScope(scope)) {
2194+
slotName = futureParentElement;
21422195
futureParentElement = cloneAttachFn;
21432196
cloneAttachFn = scope;
21442197
scope = undefined;
@@ -2150,6 +2203,16 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
21502203
if (!futureParentElement) {
21512204
futureParentElement = hasElementTranscludeDirective ? $element.parent() : $element;
21522205
}
2206+
if (slotName) {
2207+
var slotTranscludeFn = boundTranscludeFn.$$slots[slotName];
2208+
if (!slotTranscludeFn) {
2209+
throw $compileMinErr('noslot',
2210+
'No parent directive that requires a transclusion with slot name "{0}". ' +
2211+
'Element: {1}',
2212+
slotName, startingTag($element));
2213+
}
2214+
return slotTranscludeFn(scope, cloneAttachFn, transcludeControllers, futureParentElement, scopeToChild);
2215+
}
21532216
return boundTranscludeFn(scope, cloneAttachFn, transcludeControllers, futureParentElement, scopeToChild);
21542217
}
21552218
}

src/ng/directive/ngTransclude.js

+66-7
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,20 @@
88
* @description
99
* Directive that marks the insertion point for the transcluded DOM of the nearest parent directive that uses transclusion.
1010
*
11+
* You can specify that you want to insert a named transclusion slot, instead of the default slot, by providing the slot name
12+
* as the value of the `ng-transclude` or `ng-transclude-slot` attribute.
13+
*
1114
* Any existing content of the element that this directive is placed on will be removed before the transcluded content is inserted.
1215
*
1316
* @element ANY
1417
*
18+
* @param {string} ngTransclude|ngTranscludeSlot the name of the slot to insert at this point. If this is not provided or empty then
19+
* the default slot is used.
20+
*
1521
* @example
16-
<example module="transcludeExample">
22+
* ### Default transclusion
23+
* This example demonstrates simple transclusion.
24+
<example name="simpleTranscludeExample" module="transcludeExample">
1725
<file name="index.html">
1826
<script>
1927
angular.module('transcludeExample', [])
@@ -53,21 +61,72 @@
5361
</file>
5462
</example>
5563
*
56-
*/
64+
* @example
65+
* ### Multi-slot transclusion
66+
<example name="multiSlotTranscludeExample" module="multiSlotTranscludeExample">
67+
<file name="index.html">
68+
<div ng-controller="ExampleController">
69+
<input ng-model="title" aria-label="title"> <br/>
70+
<textarea ng-model="text" aria-label="text"></textarea> <br/>
71+
<pane>
72+
<pane-title><a ng-href="{{link}}">{{title}}</a></pane-title>
73+
<pane-body><p>{{text}}</p></pane-body>
74+
</pane>
75+
</div>
76+
</file>
77+
<file name="app.js">
78+
angular.module('multiSlotTranscludeExample', [])
79+
.directive('pane', function(){
80+
return {
81+
restrict: 'E',
82+
transclude: {
83+
'paneTitle': '?title',
84+
'paneBody': 'body'
85+
},
86+
template: '<div style="border: 1px solid black;">' +
87+
'<div ng-transclude="title" style="background-color: gray"></div>' +
88+
'<div ng-transclude="body"></div>' +
89+
'</div>'
90+
};
91+
})
92+
.controller('ExampleController', ['$scope', function($scope) {
93+
$scope.title = 'Lorem Ipsum';
94+
$scope.link = "https://google.com";
95+
$scope.text = 'Neque porro quisquam est qui dolorem ipsum quia dolor...';
96+
}]);
97+
</file>
98+
<file name="protractor.js" type="protractor">
99+
it('should have transcluded the title and the body', function() {
100+
var titleElement = element(by.model('title'));
101+
titleElement.clear();
102+
titleElement.sendKeys('TITLE');
103+
var textElement = element(by.model('text'));
104+
textElement.clear();
105+
textElement.sendKeys('TEXT');
106+
expect(element(by.binding('title')).getText()).toEqual('TITLE');
107+
expect(element(by.binding('text')).getText()).toEqual('TEXT');
108+
});
109+
</file>
110+
</example> */
111+
var ngTranscludeMinErr = minErr('ngTransclude');
57112
var ngTranscludeDirective = ngDirective({
58113
restrict: 'EAC',
59114
link: function($scope, $element, $attrs, controller, $transclude) {
115+
116+
function ngTranscludeCloneAttachFn(clone) {
117+
$element.empty();
118+
$element.append(clone);
119+
}
120+
60121
if (!$transclude) {
61-
throw minErr('ngTransclude')('orphan',
122+
throw ngTranscludeMinErr('orphan',
62123
'Illegal use of ngTransclude directive in the template! ' +
63124
'No parent directive that requires a transclusion found. ' +
64125
'Element: {0}',
65126
startingTag($element));
66127
}
67128

68-
$transclude(function(clone) {
69-
$element.empty();
70-
$element.append(clone);
71-
});
129+
$transclude(ngTranscludeCloneAttachFn, null, $attrs.ngTransclude || $attrs.ngTranscludeSlot);
72130
}
73131
});
132+

0 commit comments

Comments
 (0)