|
159 | 159 | * </example>
|
160 | 160 | */
|
161 | 161 | var ngTranscludeMinErr = minErr('ngTransclude');
|
162 |
| -var ngTranscludeDirective = ngDirective({ |
163 |
| - restrict: 'EAC', |
164 |
| - link: function($scope, $element, $attrs, controller, $transclude) { |
| 162 | +var ngTranscludeDirective = ['$compile', function($compile) { |
| 163 | + return { |
| 164 | + restrict: 'EAC', |
| 165 | + terminal: true, |
| 166 | + link: function($scope, $element, $attrs, controller, $transclude) { |
| 167 | + if ($attrs.ngTransclude === $attrs.$attr.ngTransclude) { |
| 168 | + // If the attribute is of the form: `ng-transclude="ng-transclude"` |
| 169 | + // then treat it like the default |
| 170 | + $attrs.ngTransclude = ''; |
| 171 | + } |
165 | 172 |
|
166 |
| - if ($attrs.ngTransclude === $attrs.$attr.ngTransclude) { |
167 |
| - // If the attribute is of the form: `ng-transclude="ng-transclude"` |
168 |
| - // then treat it like the default |
169 |
| - $attrs.ngTransclude = ''; |
170 |
| - } |
| 173 | + function ngTranscludeCloneAttachFn(clone, transcludedScope) { |
| 174 | + if (clone.length) { |
| 175 | + $element.empty(); |
| 176 | + $element.append(clone); |
| 177 | + } else { |
| 178 | + // Since this is the fallback content rather than the transcluded content, |
| 179 | + // we compile against the scope we were linked against rather than the transcluded |
| 180 | + // scope since this is the directive's own content |
| 181 | + $compile($element.contents())($scope); |
171 | 182 |
|
172 |
| - function ngTranscludeCloneAttachFn(clone) { |
173 |
| - if (clone.length) { |
174 |
| - $element.empty(); |
175 |
| - $element.append(clone); |
| 183 | + // There is nothing linked against the transcluded scope since no content was available, |
| 184 | + // so it should be safe to clean up the generated scope. |
| 185 | + transcludedScope.$destroy(); |
| 186 | + } |
176 | 187 | }
|
177 |
| - } |
178 | 188 |
|
179 |
| - if (!$transclude) { |
180 |
| - throw ngTranscludeMinErr('orphan', |
181 |
| - 'Illegal use of ngTransclude directive in the template! ' + |
182 |
| - 'No parent directive that requires a transclusion found. ' + |
183 |
| - 'Element: {0}', |
184 |
| - startingTag($element)); |
185 |
| - } |
186 |
| - |
187 |
| - // If there is no slot name defined or the slot name is not optional |
188 |
| - // then transclude the slot |
189 |
| - var slotName = $attrs.ngTransclude || $attrs.ngTranscludeSlot; |
190 |
| - $transclude(ngTranscludeCloneAttachFn, null, slotName); |
191 |
| - } |
192 |
| -}); |
| 189 | + if (!$transclude) { |
| 190 | + throw ngTranscludeMinErr('orphan', |
| 191 | + 'Illegal use of ngTransclude directive in the template! ' + |
| 192 | + 'No parent directive that requires a transclusion found. ' + |
| 193 | + 'Element: {0}', |
| 194 | + startingTag($element)); |
| 195 | + } |
193 | 196 |
|
| 197 | + // If there is no slot name defined or the slot name is not optional |
| 198 | + // then transclude the slot |
| 199 | + var slotName = $attrs.ngTransclude || $attrs.ngTranscludeSlot; |
| 200 | + $transclude(ngTranscludeCloneAttachFn, null, slotName); |
| 201 | + } |
| 202 | + }; |
| 203 | +}]; |
0 commit comments