SVGにクラスとパスを動的に追加するにはコンポーネント内にあるときに角4を入力しますか?角4 - コンポーネント内のsvgへのクラスとパスの追加?
私のコンポーネントでは、次のコードをコンポーネントテンプレートに貼り付けるとアイコンが機能します。
<svg class="another-class iconId">
<use xlink:href="/icon-path/def.svg#iconId"></use>
</svg>
しかし、私はこのようなクラスバインドしようとすると:
<svg class="another-class {{iconId}}">
<use xlink:href="/icon-path/def.svg#{{iconId}}"></use>
</svg>
を私はエラーを取得:周りに検索した後
Error: Template parse errors:
Can't bind to ':xlink:href' since it isn't a known property of ':svg:use'
、私は提案は、これにコードを変更することが見つかりました:
<svg class="another-class {{iconId}}">
<svg:use [attr.xlink:href]="/icon-path/def.svg#{{iconId}}"></svg:use>
</svg>
tha t次のエラーが表示されます。
Uncaught (in promise): Error: Template parse errors:
Parser Error: Got interpolation ({{}}) where expression was expected at column 30 in [/icon-path/def.svg#{{iconId}}]
私はここで間違っていますか?
EDIT:
<svg class="another-class {{iconId}}">
<use [attr.xlink:href]="'/icon-path/def.svg#' + iconId"></use>
</svg>
はまだ次のエラー取得:麺が提案されているようは、次のことを試みた私はエラーがない<svg class="another-class {{iconId}}">
から{{iconId}}
を削除したときにコードで
ERROR TypeError: Cannot assign to read only property 'className' of object '[object SVGSVGElement]'
を、しかし、 svgアイコンも表示されません。 AppModuleで
で[attr.xlinkを:HREF]見える= " '/アイコンパス/ def.svg#' + ICONID"'追加する必要 '{{}}'使用する場合バインディング – n00dl3
@ n00dl3こんにちは。それを試してみました。まだエラーが発生しています。私はその結果で私の答えを更新しました。 – user3607282
中括弧を忘れてしまったようです。クラスに対しては、代わりにngClassを使用してください: '[ngClass] =" iconId "'。 – n00dl3