moessi774 moessi774 - 1 month ago 16
HTML Question

Angular2 Call method in Template

How to call a method of a service in the template to create a dynamic table or something else which should be dynamic HTML? I tried with the double curled brackets with

{{anyservice.getDynamicTable(param)}}
but that is not working. What is the way to do this correctly?

Answer

You should prepare the data, that you want to display, in your controller.

That means you should

  1. call the service from your controller,
  2. create the datatable,
  3. put it in a local variable and then
  4. reference this variable from the template.

For example

dynamicTable: any;

ngOnInit() {
    this.dynamicTable = this.anyservice.getDynamicTable();
}

or with param

createDynamicTable(param) {
   this.dynamicTable = this.anyservice.getDynamicTable(param);
}
Comments