我正在使用 d3 创建图表,每个图表都必须有一个唯一的 ID。
所以我需要使用类似的东西:
<svg class="{{className}}"></svg>
className 在 component.ts 中定义 但这不起作用
当我对值进行硬编码时它起作用,但是当我在值中传递类名时它不起作用
最佳答案
class="{{className}}"
不是在 Angular 2 + 中将数据绑定(bind)到模板的正确方法。然后打开你的控制台,看看你得到了什么错误。
你应该阅读 Angular 文档:https://angular.io/api/common/NgClass .
使用<svg [ngClass]="className"></svg>
应该为你工作。
正在检查:Angular 2 add value to ngClass with interpolation如果您想了解更多有关 ngClass 和插值的知识。 检查这个:https://angular.io/guide/displaying-data查看如何以正确的方式使用插值。
关于javascript - 如何将 Angular 类名称从 component.ts 传递到 html SVG,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53807343/