我正在尝试使用 AngularJS 绑定(bind) SVG 属性。我有以下代码用于调整形状的大小 handle :
// S handle.
var s_handle = d3.select(document.createElementNS("http://www.w3.org/2000/svg", "rect"))
.attr("id", "s-resize")
.attr("x", "{{(objects['" + id + "'].w - (scope.dragHandle.width / 2))}}")
.attr("y", "{{(objects['" + id + "'].h - (scope.dragHandle.width / 2))}}")
.attr(scope.dragHandle)
.attr("cursor", "s-resize")
.call(sresize);
$compile(s_handle.node())(scope);
element.append(s_handle);
当我运行此代码时,我收到以下错误:
Error: Invalid value for <rect> attribute x="{{(objects['9b320170-6365-4f40-801d-a7a5c6b936f9'].w - (scope.dragHandle.width / 2))}}" d3.min.js:1
Error: Invalid value for <rect> attribute y="{{(objects['9b320170-6365-4f40-801d-a7a5c6b936f9'].h - (scope.dragHandle.width / 2))}}" d3.min.js:1
我尝试删除附加步骤以隔离问题 - 看起来问题实际上发生在我设置 x
和 y
属性时,甚至在 Angular 编译之前阶段。
请注意,我使用 transform
属性使此方法与另一个 SVG 对象一起使用,该属性 native 接受字符串。
以前有人遇到过这个问题吗?有什么解决办法吗?谢谢。
编辑:一直在逐步进行,看起来 $compile` 实际上并没有改变元素。会不会是元素无效?
最佳答案
看起来这与此问题有关:https://github.com/angular/angular.js/pull/2061
这个问题也解决了。
SVG 元素使用基于 XML 的模式,并且具有命名空间,因此具有验证规则。某些属性不能接受 Angular 插值指令,因为它们需要一个数值,并且浏览器会在 Angular 有机会捕获它之前验证这一点(即使元素已创建但尚未附加到 DOM)。
如果有人遇到这个问题,希望这个答案会有所帮助。
关于angularjs - D3 与 Angular 的集成 : "Error: Invalid value for <rect> attribute x",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17409841/