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

Commit c3ae6ed

Browse files
dmaslovpetebacondarwin
authored andcommitted
fix(ngTransclude): don't replace existing content if no transcluded content exists
Closes #11839
1 parent c3a2691 commit c3ae6ed

File tree

2 files changed

+61
-4
lines changed

2 files changed

+61
-4
lines changed

src/ng/directive/ngTransclude.js

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
* You can specify that you want to insert a named transclusion slot, instead of the default slot, by providing the slot name
1212
* as the value of the `ng-transclude` or `ng-transclude-slot` attribute.
1313
*
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.
1516
*
1617
* @element ANY
1718
*
@@ -62,6 +63,42 @@
6263
</example>
6364
*
6465
* @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
65102
* ### Multi-slot transclusion
66103
<example name="multiSlotTranscludeExample" module="multiSlotTranscludeExample">
67104
<file name="index.html">
@@ -120,8 +157,10 @@ var ngTranscludeDirective = ngDirective({
120157
}
121158

122159
function ngTranscludeCloneAttachFn(clone) {
123-
$element.empty();
124-
$element.append(clone);
160+
if (clone.length) {
161+
$element.empty();
162+
$element.append(clone);
163+
}
125164
}
126165

127166
if (!$transclude) {

test/ng/compileSpec.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6252,7 +6252,8 @@ describe('$compile', function() {
62526252
});
62536253

62546254

6255-
it('should clear contents of the ng-translude element before appending transcluded content', function() {
6255+
it('should clear contents of the ng-translude element before appending transcluded content' +
6256+
' if transcluded content exists', function() {
62566257
module(function() {
62576258
directive('trans', function() {
62586259
return {
@@ -6268,6 +6269,23 @@ describe('$compile', function() {
62686269
});
62696270
});
62706271

6272+
it('should NOT clear contents of the ng-translude element before appending transcluded content' +
6273+
' if transcluded content does NOT exist', function() {
6274+
module(function() {
6275+
directive('trans', function() {
6276+
return {
6277+
transclude: true,
6278+
template: '<div ng-transclude>old stuff! </div>'
6279+
};
6280+
});
6281+
});
6282+
inject(function(log, $rootScope, $compile) {
6283+
element = $compile('<div trans></div>')($rootScope);
6284+
$rootScope.$apply();
6285+
expect(sortedHtml(element.html())).toEqual('<div ng-transclude="">old stuff! </div>');
6286+
});
6287+
});
6288+
62716289

62726290
it('should throw on an ng-transclude element inside no transclusion directive', function() {
62736291
inject(function($rootScope, $compile) {

0 commit comments

Comments
 (0)