[英]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. 我正在使用d3来创建图表,每个图表都必须具有唯一的ID。
so I need to use something like: 所以我需要使用类似的东西:
<svg class="{{className}}"></svg>
className is defined in component.ts but this isn't working className在component.ts中定义,但这不起作用
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 +. class="{{className}}"
不是将数据绑定到Angular 2 +中的模板的正确方法。 And open your console to see what error did you get. 然后打开你的控制台,看看你得到了什么错误。
You should read Angular Document: https://angular.io/api/common/NgClass . 您应该阅读Angular Document: https : //angular.io/api/common/NgClass 。
use <svg [ngClass]="className"></svg>
should work for you. 使用
<svg [ngClass]="className"></svg>
应该适合你。
Checking this: Angular 2 add value to ngClass with interpolation if you want to learn more with ngClass and interpolation. 检查:如果想要了解更多ngClass和插值, Angular 2可以通过插值为ngClass添加值 。 Checking this: https://angular.io/guide/displaying-data to see how to use interpolation in a correct way.
检查: https : //angular.io/guide/displaying-data ,了解如何以正确的方式使用插值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.