Skip to content

Commit

Permalink
fix($compile): ensure transclude works at root of templateUrl
Browse files Browse the repository at this point in the history
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 angular#7183
Closes £7772
  • Loading branch information
petebacondarwin committed Jun 13, 2014
1 parent 0ebab08 commit 6393114
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/ng/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -1771,7 +1771,6 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
});
afterTemplateChildLinkFn = compileNodes($compileNode[0].childNodes, childTranscludeFn);


while(linkQueue.length) {
var scope = linkQueue.shift(),
beforeTemplateLinkNode = linkQueue.shift(),
Expand Down Expand Up @@ -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);
}
};
}
Expand Down
51 changes: 51 additions & 0 deletions test/ng/compileSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', '<div trans-sync>Content To Be Transcluded</div>');

expect(function() {
element = $compile('<div><div trans><div replace-with-template></div></div></div>')($rootScope);
$timeout.flush();
}).not.toThrow();

expect(element.text()).toEqual('Content To Be Transcluded');
});

});
});


Expand Down

0 comments on commit 6393114

Please sign in to comment.