From 63931146c095bb2f89e7a45263022dc5c06e40e3 Mon Sep 17 00:00:00 2001 From: Peter Bacon Darwin Date: Fri, 13 Jun 2014 09:24:25 +0100 Subject: [PATCH] fix($compile): ensure transclude works at root of templateUrl MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If a "replace" directive has an async template, which contains a transclusion directive at its root node, then outer transclusions were failing to be passed to this directive. An example would be uses of `ngIf` inside and outside the template. Collaborated with @caitp Closes #7183 Closes £7772 --- src/ng/compile.js | 9 +++++--- test/ng/compileSpec.js | 51 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 3 deletions(-) diff --git a/src/ng/compile.js b/src/ng/compile.js index 9311662add37..3eb8b000e86c 100644 --- a/src/ng/compile.js +++ b/src/ng/compile.js @@ -1771,7 +1771,6 @@ function $CompileProvider($provide, $$sanitizeUriProvider) { }); afterTemplateChildLinkFn = compileNodes($compileNode[0].childNodes, childTranscludeFn); - while(linkQueue.length) { var scope = linkQueue.shift(), beforeTemplateLinkNode = linkQueue.shift(), @@ -1808,13 +1807,17 @@ function $CompileProvider($provide, $$sanitizeUriProvider) { }); return function delayedNodeLinkFn(ignoreChildLinkFn, scope, node, rootElement, boundTranscludeFn) { + var childBoundTranscludeFn = boundTranscludeFn; if (linkQueue) { linkQueue.push(scope); linkQueue.push(node); linkQueue.push(rootElement); - linkQueue.push(boundTranscludeFn); + linkQueue.push(childBoundTranscludeFn); } else { - afterTemplateNodeLinkFn(afterTemplateChildLinkFn, scope, node, rootElement, boundTranscludeFn); + if (afterTemplateNodeLinkFn.transcludeOnThisElement) { + childBoundTranscludeFn = createBoundTranscludeFn(scope, afterTemplateNodeLinkFn.transclude, boundTranscludeFn); + } + afterTemplateNodeLinkFn(afterTemplateChildLinkFn, scope, node, rootElement, childBoundTranscludeFn); } }; } diff --git a/test/ng/compileSpec.js b/test/ng/compileSpec.js index 9c194b0bfc9b..7f43567a6173 100755 --- a/test/ng/compileSpec.js +++ b/test/ng/compileSpec.js @@ -4852,6 +4852,57 @@ describe('$compile', function() { expect(element.text()).toBe('-->|x|'); })); + + + // See https://github.com/angular/angular.js/issues/7183 + it("should pass transclusion through to template of a 'replace' directive", function() { + module(function() { + directive('transSync', function() { + return { + transclude: true, + link: function(scope, element, attr, ctrl, transclude) { + + expect(transclude).toEqual(jasmine.any(Function)); + + transclude(function(child) { element.append(child); }); + } + }; + }); + + directive('trans', function($timeout) { + return { + transclude: true, + link: function(scope, element, attrs, ctrl, transclude) { + + // We use timeout here to simulate how ng-if works + $timeout(function() { + transclude(function(child) { element.append(child); }); + }); + } + }; + }); + + directive('replaceWithTemplate', function() { + return { + templateUrl: "template.html", + replace: true + }; + }); + }); + + inject(function($compile, $rootScope, $templateCache, $timeout) { + + $templateCache.put('template.html', '
Content To Be Transcluded
'); + + expect(function() { + element = $compile('
')($rootScope); + $timeout.flush(); + }).not.toThrow(); + + expect(element.text()).toEqual('Content To Be Transcluded'); + }); + + }); });