【发布时间】:2014-12-24 22:33:04
【问题描述】:
我有一个指令应该在指令元素上附加一个 SVG 元素
.directive('aSvg', function ($compile) {
return {
restrict: 'E',
replace: true,
link: function (scope, element, attrs) {
var svgTag = angular.element('<svg viewBox="0 0 4000 2000" version="1.1"><defs><marker id="StartMarker" viewBox="0 0 12 12" refX="12" refY="6" markerWidth="3" markerHeight="3" stroke="green" stroke-width="2" fill="none" orient="auto"><circle cx="6" cy="6" r="5"></circle></marker><marker id="MidMarker" viewBox="0 0 10 10" refX="5" refY="5" markerUnits="strokeWidth" markerWidth="3" markerHeight="3" stroke="lightblue" stroke-width="2" fill="none" orient="auto"><path d="M 0 0 L 10 10 M 0 10 L 10 0"></path></marker><marker id="EndMarker" viewBox="0 0 10 10" refX="5" refY="5" markerUnits="strokeWidth" markerWidth="3" markerHeight="3" stroke="red" stroke-width="2" fill="none"><rect x="0" y="0" width="10" height="10"></rect></marker></defs><path d="M 200 250 L 700 100 L 900 350 L 1200 400 L 1300 200 L 1700 680 L 2200 680 L 2600 400" fill="none" stroke="black" stroke-width="50" marker-start="url(#StartMarker)" marker-mid="url(#MidMarker)" marker-end="url(#EndMarker)"></path><path d="M 1000 750 S 2000 750 2500 1250 S 1200 1000 1300 1400 S 1700 1480 1900 1200" fill="none" stroke="tomato" stroke-width="50" marker-start="url(#StartMarker)" marker-mid="url(#MidMarker)" marker-end="url(#EndMarker)"></path></svg>');
$compile(svgTag)(scope);
angular.element(svtTag).appendTo(element[0]);
}
};
});
在 html 中,我将指令作为一个元素:
<body>
<div>
<a-svg></a-svg>
</div>
</body>
但是 SVG 未正确渲染,仅显示 2 行(黑色和橙色),但没有渲染标记。示例中使用的 SVG 似乎是正确的,因为如果我将其保存到文件中,它会正确呈现,但由于某种原因,它在 AngularJS 中不起作用。
我在这里错过了什么?
提前致谢。
编辑 1:我使用的是 AngularJS v1.2.20,我尝试使用 v1.3.0 但仍然有同样的问题。
【问题讨论】:
-
不在计算机附近进行验证,但最后一行看起来很时髦。只需:
element.append(svgTag) -
仍然得到相同的结果:s。 SVG 不呈现标记。反正 Ty :)
-
对我来说看起来不错。我看到标记。还有什么不能正确渲染? plnkr.co/edit/OIJzE20G8TpZTzM3JlTm?p=preview
-
@user2943490 我注意到问题与 Angular 路由有关,我使用的是
base标签(你不在你的 plnkr 中)。感谢 plnkr,它为我指明了正确的方向。 -
由于该解决方案超出了原始问题的范围,我建议删除该问题,因为它对未来的访问者没有任何价值
标签: javascript angularjs svg