We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

Html nodes created dynamically, onclick listener of button doesn't work

I am creating HTML nodes dynamically from a component. My HTML node has a button. The onclick listener doesn't work on that except I add the function in index.html.

I don't need it in index.html. I have tried adding an onclick event by fetching the button on the basis of id and then, binding the function with one click of that button.

But it doesn't work at all. Is there an easy way to handle the onclick listener of dynamic nodes on canvas?

5 Replies

BM Balasubramanian Manikandan Syncfusion Team January 11, 2023 11:14 AM UTC

We rendered a sample for you requirement, where you can customize your code in TS file itself. Refer to the below mentioned sample and documentation link.





KB Khadeeja Basharat January 11, 2023 12:17 PM UTC


I have added a button whose listener I am trying to handle. Can you please help me with this? The button was added in the HTML node ("Click me").


GD Gobinath Dhamotharan Syncfusion Team January 12, 2023 01:48 PM UTC

Hi Khadeeja,

In the diagram, a button for each HTML node must be provided as either a string content or a ng-template in order to cause the click event to be fired. Please refer to the below code and sample for your reference.

Code snippet

app.component.html :


<ng-template #nodeTemplate let-data>






                <button>Click me</button>





app.component.ts :


  public button() {

    this.diagram.nodes[0].shape = { type: 'Basic' };

    this.diagram.nodes[0].style = { fill: 'red' };










KB Khadeeja Basharat January 12, 2023 02:03 PM UTC

We cannot use ng-template with our current structure. Can you share an example of string content?

BM Balasubramanian Manikandan Syncfusion Team January 13, 2023 06:12 AM UTC

If we render HTML content as a string, the events defined in html will not be triggered. If you need a event to be triggered, you should use ng-template to achieve your requirement. We have created a sample in which we rendered html node with string content. Refer to the below-mentioned sample and documentation.





Live Chat Icon For mobile
Up arrow icon