title: How to set template reference dynamically inside function
body: <p>I have a function below from which I am calling <code>setModelData</code>. </p>
  
  <p>My query is I want to make template variable dynamic for both <code>getGlobalList</code> and <code>getNonGlobalList</code> function.</p>
  
  <p><strong>For Example</strong></p>
  
  <p>1)  if <code>getGlobalList</code> is running it will set <strong>template: this.CustomTableItemTemplate</strong>
   inside the <code>setModelData</code> function.</p>
  
  <p>2)  if <code>getNonGlobalList</code> is running it will pass <strong>template: this.NonGlobalCustomTableItemTemplate</strong>
   inside the <code>setModelData</code> function.</p>
  
  <p>Thanks for help</p>
  
  <p><strong>Code</strong></p>
  
  <pre class="lang-js prettyprint-override"><code>@ViewChild('CustomTableItemTemplate') CustomTableItemTemplate: TemplateRef&lt;any&gt;;
  @ViewChild('NonGlobalCustomTableItemTemplate') NonGlobalCustomTableItemTemplate: TemplateRef&lt;any&gt;;
  
  
  ngOnInit() {
    this.getGlobalList();
    this.getNonGlobalList();  
  }
  
  getGlobalList() {
    this.globalSchemamodel.data.length = 0;
  
    this.Service.getGlobalList(
      this.constructQueryParam(this.globalSchemamodel, 'global'))
        .subscribe((response: any) =&gt; {
          const globalSchemas = response ? response.data : [];
          if (globalSchemas.records) {
            this.setModelData(globalSchemas, this.globalSchemamodel);
          }     
        });
  } 
  
  getNonGlobalList() {
    this.nonGlobalSchemamodel.data.length = 0;
  
    this.Service.getList(
      this.constructQueryParam(this.nonGlobalSchemamodel, 'nonglobal'))
        .subscribe((response: any) =&gt; {
          const nonglobalschemaslist = response ? response.data : [];
          if (nonglobalschemaslist.records) {
            this.setModelData(nonglobalschemaslist, this.nonGlobalSchemamodel);
          }     
        });
  
  } 
  
  setModelData(globalSchemas, globalSchemamodel) {
    for (const schema of globalSchemas.records) {
      const tableModel = [
       new TableItem({ data: schema.schema_id }),
         this.isAdminRole ? new TableItem({
         data:[{ 'schemaId': schema.schema_id }],
         **template: this.CustomTableItemTemplate**
       }) : null
      ];
      globalSchemamodel.data.push(tableModel);
    }
  }
  </code></pre>