diff --git a/src/ng/directive/ngTransclude.js b/src/ng/directive/ngTransclude.js index 805ef047ed4e..8a5037c184ea 100644 --- a/src/ng/directive/ngTransclude.js +++ b/src/ng/directive/ngTransclude.js @@ -11,7 +11,8 @@ * You can specify that you want to insert a named transclusion slot, instead of the default slot, by providing the slot name * as the value of the `ng-transclude` or `ng-transclude-slot` attribute. * - * Any existing content of the element that this directive is placed on, will be removed before the transcluded content is inserted. + * Existing content of the element that this directive is placed on will be removed before the transcluded content + * is inserted if transcluded content exists. In other case the content will be saved and used as content by default. * * @element ANY * @@ -62,6 +63,40 @@ * * @example + * ### Transclude default content + * This example shows how to use `NgTransclude` with default ng-transclude element content + + + + + + + + Button2 + + + + it('should have different transclude element content', function() { + expect(element(by.id('default')).getText()).toBe('Button1'); + expect(element(by.id('modified')).getText()).toBe('Button2'); + }); + + + * * ### Multi-slot transclusion @@ -120,8 +155,10 @@ var ngTranscludeDirective = ngDirective({ } function ngTranscludeCloneAttachFn(clone) { - $element.empty(); - $element.append(clone); + if (clone.length) { + $element.empty(); + $element.append(clone); + } } if (!$transclude) { diff --git a/test/ng/compileSpec.js b/test/ng/compileSpec.js index c353f5f9e3d4..74042ce7a984 100755 --- a/test/ng/compileSpec.js +++ b/test/ng/compileSpec.js @@ -6252,7 +6252,8 @@ describe('$compile', function() { }); - it('should clear contents of the ng-translude element before appending transcluded content', function() { + it('should clear contents of the ng-translude element before appending transcluded content' + + ' if transcluded content exists', function() { module(function() { directive('trans', function() { return { @@ -6268,6 +6269,23 @@ describe('$compile', function() { }); }); + it('should NOT clear contents of the ng-translude element before appending transcluded content' + + ' if transcluded content does NOT exist', function() { + module(function() { + directive('trans', function() { + return { + transclude: true, + template: '
old stuff!
' + }; + }); + }); + inject(function(log, $rootScope, $compile) { + element = $compile('
')($rootScope); + $rootScope.$apply(); + expect(sortedHtml(element.html())).toEqual('
old stuff!
'); + }); + }); + it('should throw on an ng-transclude element inside no transclusion directive', function() { inject(function($rootScope, $compile) {