简体   繁体   English

如何将角度从component.ts传递给html SVG

[英]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.

相关问题 如何将index.html页面中的值传递给angular4中的component.ts文件 - How to pass a value from index.html page to component.ts file in angular 4 如何将变量从一个函数传递到同一component.ts中的另一个函数 - how to pass variable from one function to another function in same component.ts in angular 6 如何在component.ts中将数据从一个函数传递到另一个函数,angular2 - how to pass data from one function to another in component.ts , angular2 如何在发射成功时处理从 service.ts 到 component.ts 的 socket.io 确认 Angular 8 - How to handle socket.io acknowledgement from service.ts to component.ts on emit success Angular 8 如何在angular的component.ts文件中传递复选框的选中和未选中值? - How to pass the checked and unchecked value of checkboxes in component.ts file in angular? 关于Angular 2-从数组中提取变量(与正面有关:html和component.ts) - Regarding Angular 2 - Extract a Variable from An Array (related to front side: html and component.ts) 将输入从component.html发送到component.ts - Send input from component.html to component.ts 如何在 HTML 中获取输入类型 Date 并在 angular7 component.ts 文件中使用? - How to take input type Date in HTML and use in angular7 component.ts file? 如何在 Angular (component.ts) 中使用 JS function - How to use JS function in Angular (component.ts) Collapsible Sidebar Angular - 当它们在 component.ts 文件中启动时,如何从指令内部检测表单事件? - Angular - How do I detect form events from inside a directive when they are initiated in component.ts file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM