In the angular documentation for the compile service (starting at line 412) there is a description of the transclude function that is passed into the linking function of a directive.
The relevant part reads:
function([scope], cloneLinkingFn, futureParentElement)
In which (line 212):
futureParentElement
: defines the parent to which thecloneLinkingFn
will add the cloned elements.
default:
$element.parent()
resp.$element
fortransclude:'element'
resp.transclude:true
.only needed for transcludes that are allowed to contain non html elements (e.g. SVG elements) and when the
cloneLinkinFn
is passed, as those elements need to created and cloned in a special way when they are defined outside their usual containers (e.g. like<svg>
).See also the
directive.templateNamespace
property.
I fail to see the point of futureParentElement
however. It says
defines the parent to which the cloneLinkingFn will add the cloned elements.
But you do that in the cloneLinkingFn
itself like this:
transclude(scope, function (clone) {
some_element.append(clone);
});
And you can't use the transclude function without defining the cloning function in the first place.
What is the proper usage/a use for futureParentElement
?
The answer to this can be found by looking at the the git blame of
compile.js
: the commit that addedfutureParentElement
is https://github.com/angular/angular.js/commit/ffbd276d6def6ff35bfdb30553346e985f4a0de6In the commit there is a test that tests a directive
svgCustomTranscludeContainer
by testing how compiling the html
<svg-custom-transclude-container><circle cx="2" cy="2" r="1"></circle>
behaves:So it looks like if you are creating an SVG image with a directive whose template wraps transcluded SVG content in
<svg> ... </svg>
tags, then that SVG image won't be valid (by some definition), if you don't pass the correctfutureParentElement
to$transclude
.Trying to see what it actually means not to be valid, beyond the test in the source code, I created 2 directives based on the ones in the unit test, and used them to try to create an SVG image with partial circle. One using the
futureParentElement
:and one that is identical but that doesn't:
As can be seen at http://plnkr.co/edit/meRZylSgNWXhBVqP1Pa7?p=preview, the one with the
futureParentElement
shows the partial circle, and the one without doesn't. The structure of the DOM appears identical. However Chrome seems to report that the secondcircle
element isn't an SVG node, but a plain HTML node.So whatever
futureParentElement
actually does under the hood, it seems to make sure that transcluded SVG content ends up being handled as SVG by the browser.