|
11 | 11 | * You can specify that you want to insert a named transclusion slot, instead of the default slot, by providing the slot name
|
12 | 12 | * as the value of the `ng-transclude` or `ng-transclude-slot` attribute.
|
13 | 13 | *
|
14 |
| - * Any existing content of the element that this directive is placed on, will be removed before the transcluded content is inserted. |
| 14 | + * Any existing content of this element will be removed before the transcluded content is inserted, |
| 15 | + * but only if the transcluded content is not empty. |
15 | 16 | *
|
16 | 17 | * @element ANY
|
17 | 18 | *
|
|
62 | 63 | </example>
|
63 | 64 | *
|
64 | 65 | * @example
|
| 66 | + * ### Transclude default content |
| 67 | + * This example shows how to use `NgTransclude` with default ng-transclude element content |
| 68 | + * |
| 69 | + * <example module="transcludeDefaultContentExample"> |
| 70 | + <file name="index.html"> |
| 71 | + <script> |
| 72 | + angular.module('transcludeDefaultContentExample', []) |
| 73 | + .directive('myButton', function(){ |
| 74 | + return { |
| 75 | + restrict: 'E', |
| 76 | + transclude: true, |
| 77 | + scope: true, |
| 78 | + template: '<button style="cursor: pointer;">' + |
| 79 | + '<ng-transclude>' + |
| 80 | + '<b style="color: red;">Button1</b>' + |
| 81 | + '</ng-transclude>' + |
| 82 | + '</button>' |
| 83 | + }; |
| 84 | + }); |
| 85 | + </script> |
| 86 | + <!-- default button content --> |
| 87 | + <my-button id="default"></my-button> |
| 88 | + <!-- modified button content --> |
| 89 | + <my-button id="modified"> |
| 90 | + <i style="color: green;">Button2</i> |
| 91 | + </my-button> |
| 92 | + </file> |
| 93 | + <file name="protractor.js" type="protractor"> |
| 94 | + it('should have different transclude element content', function() { |
| 95 | + expect(element(by.id('default')).getText()).toBe('Button1'); |
| 96 | + expect(element(by.id('modified')).getText()).toBe('Button2'); |
| 97 | + }); |
| 98 | + </file> |
| 99 | + </example> |
| 100 | + * |
| 101 | + * @example |
65 | 102 | * ### Multi-slot transclusion
|
66 | 103 | <example name="multiSlotTranscludeExample" module="multiSlotTranscludeExample">
|
67 | 104 | <file name="index.html">
|
@@ -120,8 +157,10 @@ var ngTranscludeDirective = ngDirective({
|
120 | 157 | }
|
121 | 158 |
|
122 | 159 | function ngTranscludeCloneAttachFn(clone) {
|
123 |
| - $element.empty(); |
124 |
| - $element.append(clone); |
| 160 | + if (clone.length) { |
| 161 | + $element.empty(); |
| 162 | + $element.append(clone); |
| 163 | + } |
125 | 164 | }
|
126 | 165 |
|
127 | 166 | if (!$transclude) {
|
|
0 commit comments