我有渲染SVG图标的组件:
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元素没有属性,因此大多数时间都需要属性绑定(另见
Properties and Attributes in HTML).
原文链接:https://www.f2er.com/angularjs/143120.html对于属性绑定,您需要
<use [attr.xlink:href]="iconHref">
要么
<use attr.xlink:href="{{iconHref}}">
更新
消毒可能会导致问题.
也可以看看
> https://github.com/angular/angular/issues/9510)
> https://angular.io/docs/ts/latest/api/platform-browser/index/DomSanitizationService-class.html
更新DomSanitizationService将在RC.6中重命名为DomSanitizer
更新这个应该是固定的
但是有一个开放的问题来支持命名空间属性https://github.com/angular/angular/pull/6363/files
xlink:href=""