SvgAnimatedString缺少方法indexOf

时间:2012-09-25 18:23:59

标签: javascript d3.js angularjs

我正在使用d3.jsangularjs。当在svg对象中使用超链接(通过angular指令呈现)时,我收到此错误。

enter image description here

根据文档here,svgAnimatedString没有任何特定方法。我该怎么解决这个问题。我可以注射方法或任何其他方式。

以下部分代码。谢谢你。

svg.selectAll("a.node")
                        .data(data)
                        .enter().append("a")
                        .attr("class", "node")
                        .attr("xlink:href", "test")
                        .append("rect")

4 个答案:

答案 0 :(得分:6)

如果必须处理不处理SVG图形的库,可以使用以下内容在SVGAnimatedString上定义split。冲洗并重复其他字符串原型方法。

 // Set a split prototype on the SVGAnimatedString object which will return the split of the baseVal on this
 // SVGAnimatedString instance
 SVGAnimatedString.prototype.split = function(){ return String.prototype.split.apply(this.baseVal, arguments); };

答案 1 :(得分:4)

各种图书馆都遇到过这个问题(即here)。在SVG中,当您尝试获取锚点的href属性时,它将返回SVGAnimatedString。您可以使用SVGAnimatedString.baseVal获取实际字符串。我不知道如何在不修补Angular的情况下做到这一点,但这会让你知道需要什么:

  this.$$rewriteAppUrl = function(absoluteLinkUrl) {
     if (absoluteLinkUrl.baseVal != null) {
        absoluteLinkUrl = absoluteLinkUrl.baseVal;
     }
    if(absoluteLinkUrl.indexOf(appBaseUrl) == 0) {
      return absoluteLinkUrl;
    }
  }

答案 2 :(得分:3)

这是一个简单的Shim,可以使用现有代码:

SVGAnimatedString.prototype.toString = function () { return this.baseVal; }

答案 3 :(得分:0)

我没有使用d3也有同样的问题。我只使用纯SVG并使用angularJS控制器将其附加到DOM中。问题不在d3中。您可以通过在包含链接的元素上的jQuery单击事件处理程序中添加return false值来解决此问题。

我的SVG的一部分:

<g id="1" class="node">
<a xlink:title="title">
<polygon fill="#cccccc" stroke="#cccccc" points="25,-434 25,-457 98,-457 98,-434 25,-434"></polygon>
<polygon fill="none" stroke="black" points="25,-434 25,-457 98,-457 98,-434 25,-434"></polygon>
<text text-anchor="start" x="29.5" y="-442.3" font-family="Arial" font-size="14.00">title</text>
<polygon fill="transparent" stroke="black" points="25,-412 25,-434 98,-434 98,-412 25,-412"></polygon>
<text text-anchor="start" x="37" y="-419.8" font-family="Arial" font-size="14.00">title</text>
</a>
</g>

jQuery重载方法:

$('g.node').click(function (element) {
   return false;
});

希望这会有所帮助......