HTML Code which is dynamically added on DOM from.
<ul>
<li>
<button (click)="Onclick(abc)" class="btn btn-sm btn-danger click_for_save pull-right" type="button">
<i class="fa fa-close"></i> Save </button>
</li>
</ul>
Here in HTML code there is a click event with parameters. (click)="Onclick(abc)"
Now I'm addind it dynamically. So, click event is not working.
I have search a lot for it but not able to find the solution for it. My typescript code is below.
import { AfterViewInit, Component, ElementRef} from '@angular/core';
constructor(private elementRef:ElementRef) {}
// Below code I have added just after the dynamically added code.
this.elementRef.nativeElement.querySelector('.click_for_save')
.addEventListener('click', this.click_for_save.bind(this));
Onclick(abc) {
console.log(abc);
}
I'm getting this error.
ERROR TypeError: Cannot read property 'addEventListener' of null
But click event is not firing. I have tried even with SetTimeOut()
function with 3 second gap.
EDIT :-
I'm checking with this,
let query_selector_value = this.elementRef.nativeElement.querySelector('.click_for_save');
if(query_selector_value){
query_selector_value.addEventListener('click', this.click_for_save.bind(this));
}
And I'm getting query_selector_value - null