Angular2 SVG xlink:href [英] Angular2 SVG xlink:href

查看:32
本文介绍了Angular2 SVG xlink:href的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有用于渲染 SVG 图标的组件:

I have component for rendering SVG icon:

import {Component, Directive} from 'angular2/core';
import {COMMON_DIRECTIVES} from 'angular2/common';

@Component({
  selector: '[icon]',
  directives: [COMMON_DIRECTIVES],
  template: `<svg role="img" class="o-icon o-icon--large">
                <use [xlink:href]="iconHref"></use>
              </svg>{{ innerText }}`
})
export class Icon {
  iconHref: string = 'icons/icons.svg#menu-dashboard';
  innerText: string = 'Dashboard';
}

这会触发错误:

EXCEPTION: Template parse errors:
  Can't bind to 'xlink:href' since it isn't a known native property ("<svg role="img" class="o-icon o-icon--large">
<use [ERROR ->][xlink:href]=iconHref></use>
  </svg>{{ innerText }}"): SvgIcon@1:21

如何设置动态xlink:href?

推荐答案

SVG 元素没有属性,因此大多数时候需要属性绑定(另请参见 HTML 中的属性和属性).

SVG elements doen't have properties, therefore attribute binding is required most of the time (see also Properties and Attributes in HTML).

对于属性绑定,您需要

<use [attr.xlink:href]="iconHref">

<use attr.xlink:href="{{iconHref}}">

更新

消毒可能会导致问题.

另见

更新 DomSanitizationService 将在 RC.6 中更名为 DomSanitizer

Update DomSanitizationService is going to be renamed to DomSanitizer in RC.6

更新这应该是固定的

但是对于命名空间属性https://github,有一个开放的问题来支持这一点.com/angular/angular/pull/6363/files

作为解决方法添加额外的

As work-around add an additional

xlink:href=""

Angular 可以更新属性,但在添加时有问题.

Angular can update the attribute but has issues with adding.

如果 xlink:href 实际上是一个属性,那么您的语法在添加 PR 后也应该有效.

If xlink:href is actually a property then your syntax should work after the PR was added as well.

这篇关于Angular2 SVG xlink:href的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆