用AngularJS解析SVG
我对AngularJS和SVG都是新手,所以如果我做错了什么,我道歉 我正在尝试使用AngularJS创建SVG模式: 代码小提琴: 模板:用AngularJS解析SVG,angularjs,svg,Angularjs,Svg,我对AngularJS和SVG都是新手,所以如果我做错了什么,我道歉 我正在尝试使用AngularJS创建SVG模式: 代码小提琴: 模板: <svg width="100%" height="100%" xmlns="http://www.w3.org/2000/svg"> <defs> <pattern id="grid" width="{{cubeWidth}}" height="{{cubeHeight}}" patternUnit
<svg width="100%" height="100%" xmlns="http://www.w3.org/2000/svg">
<defs>
<pattern id="grid" width="{{cubeWidth}}" height="{{cubeHeight}}" patternUnits="userSpaceOnUse">
<path d="M 0 0 L 0 {{cubeHeight}}" fill="none" stroke="gray" stroke-width="1" stroke-opacity="0.5"/>
<path d="M 0 0 L {{cubeWidth}} 0" fill="none" stroke="gray" stroke-width="1" stroke-opacity="0.5"/>
<!--<rect width="80" height="80" stroke="red" stroke-width="20" stroke-opacity="0.5" fill="white"/>-->
</pattern>
</defs>
<rect width="100%" height="100%" fill="url(#grid)"/>
</svg>
它似乎可以工作,但我得到一个控制台错误:
知道为什么吗?SVG解析发生在AngularJS可以设置任何变量之前。您可以尝试以编程方式创建SVG:
ng attr-
作为所需属性的前缀来使用它。它将等待,直到绑定计算有效并添加具有适当值的实属性。
在您的情况下,它将是:
<svg width="100%" height="100%" xmlns="http://www.w3.org/2000/svg">
<defs>
<pattern id="grid" ng-attr-width="{{cubeWidth}}" ng-attr-height="{{cubeHeight}}" patternUnits="userSpaceOnUse">
<path ng-attr-d="M 0 0 L 0 {{cubeHeight}}" fill="none" stroke="gray" stroke-width="1" stroke-opacity="0.5"/>
<path ng-attr-d="M 0 0 L {{cubeWidth}} 0" fill="none" stroke="gray" stroke-width="1" stroke-opacity="0.5"/>
<!--<rect width="80" height="80" stroke="red" stroke-width="20" stroke-opacity="0.5" fill="white"/>-->
</pattern>
</defs>
<rect width="100%" height="100%" fill="url(#grid)"/>
</svg>
应该没有错误了。请记住更新您的AngularJS版本。可能已经太晚了,无法具体帮助您,但我刚刚发表了一篇关于AngularJS+SVG的文章。
<svg width="100%" height="100%" xmlns="http://www.w3.org/2000/svg">
<defs>
<pattern id="grid" ng-attr-width="{{cubeWidth}}" ng-attr-height="{{cubeHeight}}" patternUnits="userSpaceOnUse">
<path ng-attr-d="M 0 0 L 0 {{cubeHeight}}" fill="none" stroke="gray" stroke-width="1" stroke-opacity="0.5"/>
<path ng-attr-d="M 0 0 L {{cubeWidth}} 0" fill="none" stroke="gray" stroke-width="1" stroke-opacity="0.5"/>
<!--<rect width="80" height="80" stroke="red" stroke-width="20" stroke-opacity="0.5" fill="white"/>-->
</pattern>
</defs>
<rect width="100%" height="100%" fill="url(#grid)"/>
</svg>