|
8 | 8 | * @description
|
9 | 9 | * Directive that marks the insertion point for the transcluded DOM of the nearest parent directive that uses transclusion.
|
10 | 10 | *
|
| 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 | + * |
11 | 14 | * Any existing content of the element that this directive is placed on will be removed before the transcluded content is inserted.
|
12 | 15 | *
|
13 | 16 | * @element ANY
|
14 | 17 | *
|
| 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 | + * |
15 | 21 | * @example
|
16 |
| - <example module="transcludeExample"> |
| 22 | + * ### Default transclusion |
| 23 | + * This example demonstrates simple transclusion. |
| 24 | + <example name="simpleTranscludeExample" module="transcludeExample"> |
17 | 25 | <file name="index.html">
|
18 | 26 | <script>
|
19 | 27 | angular.module('transcludeExample', [])
|
|
53 | 61 | </file>
|
54 | 62 | </example>
|
55 | 63 | *
|
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'); |
57 | 112 | var ngTranscludeDirective = ngDirective({
|
58 | 113 | restrict: 'EAC',
|
59 | 114 | link: function($scope, $element, $attrs, controller, $transclude) {
|
| 115 | + |
| 116 | + function ngTranscludeCloneAttachFn(clone) { |
| 117 | + $element.empty(); |
| 118 | + $element.append(clone); |
| 119 | + } |
| 120 | + |
60 | 121 | if (!$transclude) {
|
61 |
| - throw minErr('ngTransclude')('orphan', |
| 122 | + throw ngTranscludeMinErr('orphan', |
62 | 123 | 'Illegal use of ngTransclude directive in the template! ' +
|
63 | 124 | 'No parent directive that requires a transclusion found. ' +
|
64 | 125 | 'Element: {0}',
|
65 | 126 | startingTag($element));
|
66 | 127 | }
|
67 | 128 |
|
68 |
| - $transclude(function(clone) { |
69 |
| - $element.empty(); |
70 |
| - $element.append(clone); |
71 |
| - }); |
| 129 | + var slotName = $attrs.ngTransclude || $attrs.ngTranscludeSlot; |
| 130 | + if (slotName) { |
| 131 | + $transclude = $transclude.$$boundTransclude.$$slots[slotName]; |
| 132 | + if ($transclude) { |
| 133 | + $transclude(undefined, ngTranscludeCloneAttachFn); |
| 134 | + } else { |
| 135 | + throw ngTranscludeMinErr('orphan', |
| 136 | + 'Illegal use of ngTransclude directive in the template! ' + |
| 137 | + 'No parent directive that requires a transclusion with slot name "{0}". ' + |
| 138 | + 'Element: {1}', |
| 139 | + slotName, startingTag($element)); |
| 140 | + } |
| 141 | + } else { |
| 142 | + $transclude(ngTranscludeCloneAttachFn); |
| 143 | + } |
72 | 144 | }
|
73 | 145 | });
|
| 146 | + |
0 commit comments