Set (click) attribute in button using renderer2 in Angular

  angular, angular-renderer2, html, typescript

I am trying to create a chat-based application where I am creating each type of chat popup according to requirements and using Angular Renderer2. I tried to add a (click) attribute to the chat message element while rendering that in UI.

I got this error:

DOMException: Failed to execute ‘setAttribute’ on ‘Element’:
‘((click))’ is not a valid attribute name.

chat.component.ts

const btn3: HTMLDivElement = this.renderer.createElement('button');
this.renderer.addClass(btn3, 'btn');
this.renderer.addClass(btn3, 'btn-light');
this.renderer.setAttribute(btn3, '(click)', 'sendMessage()');
btn3.innerHTML = 'Maybe later!';
this.renderer.appendChild(buttons, btn3);

enter image description here

Source: Angular Questions

One Reply to “Set (click) attribute in button using renderer2 in Angular”

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.