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

Commit eed299a

Browse files
committed
fix(ngTransclude): clear the translusion point before transcluding
when the transluded content is being teleported to the translusion point, we should ensure that the translusion point is empty before appending otherwise we end up with junk before the transcluded content
1 parent bf79bd4 commit eed299a

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

src/ng/directive/ngTransclude.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
* @name ng.directive:ngTransclude
66
*
77
* @description
8-
* Insert the transcluded DOM here.
8+
* Directive that marks the insertion point for the transcluded DOM of the nearest parent directive that uses transclusion.
9+
*
10+
* Any existing content of the element that this directive is placed on will be removed before the transcluded content is inserted.
911
*
1012
* @element ANY
1113
*
@@ -58,6 +60,7 @@ var ngTranscludeDirective = ngDirective({
5860

5961
link: function($scope, $element, $attrs, controller) {
6062
controller.$transclude(function(clone) {
63+
$element.html('');
6164
$element.append(clone);
6265
});
6366
}

test/ng/compileSpec.js

+20-2
Original file line numberDiff line numberDiff line change
@@ -2450,7 +2450,7 @@ describe('$compile', function() {
24502450
element = $compile('<div parent-directive><div child-directive></div>childContentText;</div>')($rootScope);
24512451
$rootScope.$apply();
24522452
expect(log).toEqual('parentController; childController');
2453-
expect(element.text()).toBe('parentTemplateText;childTemplateText;childContentText;')
2453+
expect(element.text()).toBe('childTemplateText;childContentText;')
24542454
});
24552455
});
24562456

@@ -2554,7 +2554,7 @@ describe('$compile', function() {
25542554
'</div>')($rootScope);
25552555
$rootScope.$apply();
25562556
expect(log).toEqual('parentController; childController; babyController');
2557-
expect(element.text()).toBe('parentTemplateText;childTemplateText;childContentText;babyTemplateText;')
2557+
expect(element.text()).toBe('childContentText;babyTemplateText;')
25582558
});
25592559
});
25602560

@@ -2825,6 +2825,24 @@ describe('$compile', function() {
28252825
});
28262826

28272827

2828+
it('should clear contents of the ng-translude element before appending transcluded content',
2829+
function() {
2830+
module(function() {
2831+
directive('trans', function() {
2832+
return {
2833+
transclude: true,
2834+
template: '<div ng-transclude>old stuff! </div>'
2835+
};
2836+
});
2837+
});
2838+
inject(function(log, $rootScope, $compile) {
2839+
element = $compile('<div trans>unicorn!</div>')($rootScope);
2840+
$rootScope.$apply();
2841+
expect(sortedHtml(element.html())).toEqual('<div ng-transclude=""><span>unicorn!</span></div>');
2842+
});
2843+
});
2844+
2845+
28282846
it('should make the result of a transclusion available to the parent directive in post-linking phase (template)',
28292847
function() {
28302848
module(function() {

0 commit comments

Comments
 (0)