angular - SVGforeignObject未显示在 Angular 模板内

标签 angular svg

我在 Angular9 组件模板中使用内联 SVG。我想在这个 SVG 中显示一个 HTML 元素。当我尝试在其中使用 SVGforeignObject 时,它不显示。

<p>Inline SVG with foreignobject inside angular component:</p>
<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>

当我在其他地方使用相同的 SVG 代码时,它可以正常工作。例如,当我直接在index.html(而不是 Angular 组件)中插入以下代码时,它会正确显示:

<p>SVG with foreignobject in index.html (not in angular component):</p>
<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" >
    <div xmlns="http://www.w3.org/1999/xhtml" style="background-color: aqua;">
      foreign object
    </div> 
  </svg:foreignobject> 
</svg>

我遗漏了一些东西,还是 Angular 内部的错误?请参阅此处的示例:https://stackblitz.com/edit/angular-frdbxq

最佳答案

如果是 Angular 模板,您应该注意编写标签时区分大小写的方式:

<foreignobject 
        ||
        \/
<foreignObject 

<强> Forked Stackblitz

关于angular - SVGforeignObject未显示在 Angular 模板内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60703834/

相关文章:

javascript - 引用 ngFor 中的外部组件

angular - *ngFor... 以循环中的特定元素为目标(带有事件)

angular - 如何获取路由数据解析值?

javascript - Angular 2 注入(inject)器层次结构和 NgModule

reactjs - 如何在 React 中导入 svg 文件?

javascript - SVG 中 Firefox 中的 onmouseover 事件

一页上的SVG重复ID

angular - 迁移到 Angular 15 后,上下文从 test.ts 中删除

javascript - 当后台 JS 负载过重时,SVG 加载器将无法正确显示动画。这可以修复吗?

svg - 我如何知道我的浏览器是否支持SVG 2.0?