繁体   English   中英

Angular 2 如何加载 SVG 图像?

[英]How can Angular 2 load an SVG image?

我在 Angular 2 模板中尝试了这段代码:

<ul class="products">
  <li *ngFor="#p of products">
    <svg width="200" height="100" xmlns="http://www.w3.org/2000/svg"
         xmlns:svg="http://www.w3.org/2000/svg"
         xmlns:xlink="http://www.w3.org/1999/xlink">
         <g>
            <title>{{p.Name}}</title>
            <image xlink:href="http://somewhere.com/{{p.Picture1}}"
                   id="svg_21" height="176.731579" width="117" y="28" x="32" />
        </g>
    </svg></li></ul>

并得到错误:

 EXCEPTION: Template parse errors:
 Can't bind to '@xlink:href' since it isn't a known native property 
 <image [ERROR ->]xlink:href="http://somewhere.com/{{p.Picture1}}"
        id="svg_21" height="176.731579" widt"): AppComponent@13:37

我尝试使用它,这绝对是正确的路径:

attr.xlink:href="http://somewhere.com/{{p.Picture1}}"

但是在使用它时,我反而得到了 not find my-app的错误。

发布问题几秒钟后,我找到了答案:

<image *ngIf="p.Picture1" attr.xlink:href="http://somewhere.com/{{p.Picture1}}" />

显然,如果p.Picture1为空,它更有可能使 html 或 SVG 无效,从而导致找不到my-app ,如下所示。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM