I want to create a dynamic number of html dom element buttons and have them be able to call a service when clicked. Is there any way to do that?
For example, test.component.ts:
import {ApiService } from '../services/api.service';
...
export class test implement OnInit(){
constructor(private apiGet : ApiService) { }
ngOnInit() {
...
for (i = 0; i < x; i++) {
var row = document.getElementById('row');
var cell = insertCell();
cell.innerHTML = "<button name='" + i + '" (click)=test.callService()>click me</button>";
}
}
public callService() {
//call ApiService here using buttons name
}
}
I've been getting it to work when I put the (click) event on an html element inside test.component.html, but it hasn't been working for the dom generated buttons. Is there something I'm missing, or is there a better way to do this?