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

Commit c063544

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

File tree

2 files changed

+62
-7
lines changed

2 files changed

+62
-7
lines changed

src/ng/directive/ngTransclude.js

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +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-
* For required slots and the default transclusion, existing content will be removed before the transcluded content is inserted.
15-
*
16-
* For optional slots, existing content is left in place, if the slot was not filled.
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.
1716
*
1817
* @element ANY
1918
*
@@ -64,6 +63,42 @@
6463
</example>
6564
*
6665
* @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
67102
* ### Multi-slot transclusion
68103
<example name="multiSlotTranscludeExample" module="multiSlotTranscludeExample">
69104
<file name="index.html">
@@ -124,8 +159,10 @@ var ngTranscludeDirective = ngDirective({
124159
}
125160

126161
function ngTranscludeCloneAttachFn(clone) {
127-
$element.empty();
128-
$element.append(clone);
162+
if (clone.length) {
163+
$element.empty();
164+
$element.append(clone);
165+
}
129166
}
130167

131168
if (!$transclude) {

test/ng/compileSpec.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33

4-
ddescribe('$compile', function() {
4+
describe('$compile', function() {
55
function isUnknownElement(el) {
66
return !!el.toString().match(/Unknown/);
77
}
@@ -6252,7 +6252,8 @@ ddescribe('$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 @@ ddescribe('$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)