Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用AngularJS解析SVG_Angularjs_Svg - Fatal编程技术网

用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

我对AngularJS和SVG都是新手,所以如果我做错了什么,我道歉

我正在尝试使用AngularJS创建SVG模式:

代码小提琴:

模板:

<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:


问题在于,在angular能够执行任何操作之前,svg正在被解析,因此在angular到达之前,带双大括号的值无效。Angular的团队增加了一种定义某种“延迟绑定”的方法。您可以通过使用
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>