I have an angular ag-grid, from where I have created a cellRenderer and cellRendererParams. And from cellRenderer I am calling a method which creates a button in that every cell of ag-grid.
constructor(private notificationService: NotificationService) { }
ngOnInit() {
this.columnDefs = [{
headerName: "Action",
field: "notificationId",
width: 180,
cellRenderer: this.notificationCellRendererFunc,
cellRendererParams: {
notificationId: 'notificationId'
}
}];
}
And the notificationCellRendererFunc:
notificationCellRendererFunc(params) {
var self = this;
var eSpan = document.createElement('button');
console.log(params.value); // logs notificationId
eSpan.innerHTML = 'Resend';
eSpan.id = params.value;
eSpan.addEventListener('click', function (eSpan) {
alert(eSpan.toElement.id);
var notificationFilter: any = {};
notificationFilter.notificationId = eSpan.toElement.id;
self.notificationService.ResendNotification(notificationFilter)
.subscribe(
(data: any) => {
console.log('in save')
},
err => { console.log(err) }, // error
);
});
return eSpan;
}
In the above method, I am creating eventListener for every button, so that when any of the button hits, it will provide me the selected row's notificationId and I can send it to the API for further processing.
But the problem is, 'this' keyword is not working inside eventListener, even if I assign 'this' to 'self' keyword outside the listener. It says: ERROR TypeError: Cannot read property 'notificationService' of undefined at HTMLButtonElement..
"My moto is to create a button in every row of ag-grid, and after hitting the button it will resend the notification."