可以通过查看git blame of compile.js 找到答案:添加futureParentElement 的提交是https://github.com/angular/angular.js/commit/ffbd276d6def6ff35bfdb30553346e985f4a0de6
在提交中有一个测试指令svgCustomTranscludeContainer
directive('svgCustomTranscludeContainer', function() {
return {
template: '<svg width="400" height="400"></svg>',
transclude: true,
link: function(scope, element, attr, ctrls, $transclude) {
var futureParent = element.children().eq(0);
$transclude(function(clone) {
futureParent.append(clone);
}, futureParent);
}
};
});
通过测试编译 html <svg-custom-transclude-container><circle cx="2" cy="2" r="1"></circle> 的行为:
it('should handle directives with templates that manually add the transclude further down', inject(function() {
element = jqLite('<div><svg-custom-transclude-container>' +
'<circle cx="2" cy="2" r="1"></circle></svg-custom-transclude-container>' +
'</div>');
$compile(element.contents())($rootScope);
document.body.appendChild(element[0]);
var circle = element.find('circle');
assertIsValidSvgCircle(circle[0]);
}));
因此,如果您使用指令创建 SVG 图像,该指令的模板将转置的 SVG 内容包装在 <svg> ... </svg> 标签中,那么如果您不通过,该 SVG 图像将无效(根据某种定义)正确的futureParentElement 到$transclude。
除了源代码中的测试之外,为了了解无效的实际含义,我根据单元测试中的指令创建了 2 个指令,并使用它们尝试创建带有部分圆形的 SVG 图像。一个使用futureParentElement:
<div><svg-custom-transclude-container-with-future><circle cx="1" cy="2" r="20"></circle></svg-custom-transclude-container></div>
一个相同但不同的:
<div><svg-custom-transclude-container-without-future><circle cx="2" cy="2" r="20"></circle></svg-custom-transclude-container></div>
从http://plnkr.co/edit/meRZylSgNWXhBVqP1Pa7?p=preview 可以看出,带有futureParentElement 的那个显示了部分圆圈,而没有那个没有。 DOM 的结构看起来相同。然而,Chrome 似乎报告第二个 circle 元素不是 SVG 节点,而是纯 HTML 节点。
因此,无论futureParentElement 在幕后实际做了什么,它似乎确保了嵌入的 SVG 内容最终被浏览器作为 SVG 处理。