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

Commit 398053c

Browse files
fix($compile): ensure transclude works at root of templateUrl
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
1 parent 0ebab08 commit 398053c

File tree

2 files changed

+57
-3
lines changed

2 files changed

+57
-3
lines changed

src/ng/compile.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -1771,7 +1771,6 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
17711771
});
17721772
afterTemplateChildLinkFn = compileNodes($compileNode[0].childNodes, childTranscludeFn);
17731773

1774-
17751774
while(linkQueue.length) {
17761775
var scope = linkQueue.shift(),
17771776
beforeTemplateLinkNode = linkQueue.shift(),
@@ -1808,13 +1807,17 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
18081807
});
18091808

18101809
return function delayedNodeLinkFn(ignoreChildLinkFn, scope, node, rootElement, boundTranscludeFn) {
1810+
var childBoundTranscludeFn = boundTranscludeFn;
18111811
if (linkQueue) {
18121812
linkQueue.push(scope);
18131813
linkQueue.push(node);
18141814
linkQueue.push(rootElement);
1815-
linkQueue.push(boundTranscludeFn);
1815+
linkQueue.push(childBoundTranscludeFn);
18161816
} else {
1817-
afterTemplateNodeLinkFn(afterTemplateChildLinkFn, scope, node, rootElement, boundTranscludeFn);
1817+
if (afterTemplateNodeLinkFn.transcludeOnThisElement) {
1818+
childBoundTranscludeFn = createBoundTranscludeFn(scope, afterTemplateNodeLinkFn.transclude, boundTranscludeFn);
1819+
}
1820+
afterTemplateNodeLinkFn(afterTemplateChildLinkFn, scope, node, rootElement, childBoundTranscludeFn);
18181821
}
18191822
};
18201823
}

test/ng/compileSpec.js

+51
Original file line numberDiff line numberDiff line change
@@ -4852,6 +4852,57 @@ describe('$compile', function() {
48524852

48534853
expect(element.text()).toBe('-->|x|');
48544854
}));
4855+
4856+
4857+
// See https://github.com/angular/angular.js/issues/7183
4858+
it("should pass transclusion through to template of a 'replace' directive", function() {
4859+
module(function() {
4860+
directive('transSync', function() {
4861+
return {
4862+
transclude: true,
4863+
link: function(scope, element, attr, ctrl, transclude) {
4864+
4865+
expect(transclude).toEqual(jasmine.any(Function));
4866+
4867+
transclude(function(child) { element.append(child); });
4868+
}
4869+
};
4870+
});
4871+
4872+
directive('trans', function($timeout) {
4873+
return {
4874+
transclude: true,
4875+
link: function(scope, element, attrs, ctrl, transclude) {
4876+
4877+
// We use timeout here to simulate how ng-if works
4878+
$timeout(function() {
4879+
transclude(function(child) { element.append(child); });
4880+
});
4881+
}
4882+
};
4883+
});
4884+
4885+
directive('replaceWithTemplate', function() {
4886+
return {
4887+
templateUrl: "template.html",
4888+
replace: true
4889+
};
4890+
});
4891+
});
4892+
4893+
inject(function($compile, $rootScope, $templateCache, $timeout) {
4894+
4895+
$templateCache.put('template.html', '<div trans-sync>Content To Be Transcluded</div>');
4896+
4897+
expect(function() {
4898+
element = $compile('<div><div trans><div replace-with-template></div></div></div>')($rootScope);
4899+
$timeout.flush();
4900+
}).not.toThrow();
4901+
4902+
expect(element.text()).toEqual('Content To Be Transcluded');
4903+
});
4904+
4905+
});
48554906
});
48564907

48574908

0 commit comments

Comments
 (0)