簡體   English   中英

AngularJS無法正確渲染SVG

[英]AngularJS not rendering SVG correctly

我有一條指令,應該將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無法正確渲染,僅顯示兩行(黑色和橙色),但未渲染任何標記。 該示例中使用的SVG似乎是正確的,因為如果我將其保存到文件中,它可以正確呈現,但是由於某種原因,它在AngularJS中不起作用。

我在這里想念什么?

提前致謝。

編輯1:我使用的是AngularJS v1.2.20,我嘗試使用v1.3.0,但仍然存在相同的問題。

經過更多研究后,我發現問題出在Angular路由中,更具體地說是<base href='/'>標簽與SVG標記中定義的鏈接搞混了。

我通過更改每個路徑的markers屬性中的url解決了我的問題(請注意marker-startmarker-midmarker-end屬性中的/ myroute ):

<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(/myroute#StartMarker)" marker-mid="url(/myroute#MidMarker)" marker-end="url(/myroute#EndMarker)"></path>

有關更多詳細信息,我建議在此處閱讀該問題的可接受答案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM