在Angular7中使用SVG的foreignObject标签

5
在Angular 7中,我想使用SVG的foreignObject标签插入一些HTML代码。但是它没有生效。终端报告了一个错误。Angular无法识别HTML标签。

这是代码

<g id="peizhi" (click)="config()" style="width: 110px; height: 30px" transform="translate(181,0)">
<!--      <a (click)="config()" style="width: 110px;height: 30px">-->
        <use xlink:href="#Rectangle" x="0" y="0"></use>
        <text x="15" y="10" style="fill: rgba(245, 245, 245, 1);width: 24px;height: 27px;" font-size="12px">
          配置
        </text>
        <polygon  points="41 0, 47 0, 44 4" fill="rgba(216, 216, 216, 1)"/>

      <foreignObject width="100" height="50"
                     requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility">
        <!-- XHTML content goes here -->
        <body xmlns="http://www.w3.org/1999/xhtml">
        <p>Here is a paragraph that requires word wrap</p>
        </body>
      </foreignObject>
<!--      </a>-->
    </g>

这是错误信息

ERROR Error: Uncaught (in promise): Error: Template parse errors:
':svg:p' is not a known element:
1. If ':svg:p' is an Angular component, then verify that it is part of this module.
2. To allow any element add 'NO_ERRORS_SCHEMA' to the '@NgModule.schemas' of this component. ("     <!-- XHTML content goes here -->
        <body xmlns="http://www.w3.org/1999/xhtml">
        [ERROR ->]<p>Here is a paragraph that requires word wrap</p>
        </body>
      </foreignObject>
"): ng:///OperateNavModule/EndmonitorNavComponent.html@49:8
':svg:body' is not a known element:
2个回答

9

您需要将xhtml命名空间添加到段落中。像这样:

<xhtml:p>Here is a paragraph that requires word wrap</p>

您可以在这里尝试它。

3
<ng-template #nodeTemplate let-node>
  <svg:g class="node">
    <svg:foreignObject>
      <div>Your HTML here</div>
    </svg:foreignObject>
  </svg:g>
</ng-template>

或者

<svg xmlns="http://www.w3.org/2000/svg" width="200px" height="100px">
  <rect x="0" y="0" width="100%" height="100%" fill="yellow"/>
  <foreignObject  x="10" y="10" width="100px" height="50px" >
    <xhtml:div xmlns="http://www.w3.org/1999/xhtml" style="background-color: aqua;">
      foreign object
    </xhtml:div> 
  </foreignObject> 
</svg>

我们可以像这样在Angular中使用foreignObject。


感谢您的回答。如果有人正在使用d3来绘制元素,类似的解决方案是ele.append("xhtml:div")就可以了。 - lazurey

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接