getCheckStatus(checkStatus: string) {
let statusClass = '';
if (checkStatus === 'Completed') {
statusClass = 'mat-focus-indicator btn--primary__button mat-raised-button mat-button-base';
} else {
statusClass = 'mat-focus-indicator mat-raised-button mat-button-base mat-button-disabled';
}
const button = document.createElement('button');
button.className = statusClass;
button.textContent = 'Close';
button.addEventListener("click", () => this.popUp());
const container = document.createElement('div');
container.appendChild(button);
return container.innerHTML;
}
popUp() {console.log('Finally this is working');}
I’m trying to call this method by adding addEventListner in my Angular Project. But this is not working, this popUp() method is never called.
MyAssumption: this is a JavaScript code, but we have to write in typescript in Angular, but I can’t find any different syntax especially for typescript.
2
Answers
In an Angular project, you typically don’t use addEventListener directly on DOM elements. Instead, you can use Angular’s event binding to attach methods to DOM events in your component’s template.
Here’s an example of how you can handle a click event in an Angular component:
In app.component.ts:
In component.html :
Therefore your assumption is wrong. Infact you does not need
addEventListener
in angular. Refer the above sample to get it in working state.You can try this: