简体   繁体   中英

How to pass a class name in angular from component.ts to html SVG

I'm using d3 to create charts and every chart has to have a unique id.

so I need to use something like:

<svg class="{{className}}"></svg>

className is defined in component.ts but this isn't working

it works when I hard code the value, but when I pass in the class name in the value it doesn't work

class="{{className}}" is not the right way to bind data into template in Angular 2 +. And open your console to see what error did you get.

You should read Angular Document: https://angular.io/api/common/NgClass .

use <svg [ngClass]="className"></svg> should work for you.

Checking this: Angular 2 add value to ngClass with interpolation if you want to learn more with ngClass and interpolation. Checking this: https://angular.io/guide/displaying-data to see how to use interpolation in a correct way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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