Creating versatile ngTemplates in Angular 7

Currently I am working on an Angular 7 app. In this project, I have a gridComponent which is being used in my app.component.html. My goal is to dynamically create columns from within my app.component.ts. However, the issue I am facing is that the student and manager columns are appearing empty. What could be causing this problem? I've included the final version of how I want the grid to look at the bottom of the page.

Visit STACKBLITZ for more details.

Answer №1

To simplify the process, consider utilizing a custom directive such as:

cell-template.directive.ts

import { Directive, Input, TemplateRef } from '@angular/core';

@Directive({
  selector: 'ng-template[cellTemplate]'
})
export class CellTemplateDirective {
  @Input('cellTemplate') name: string;

  constructor(public template: TemplateRef<any>) {}
}

With this directive, you can specify templates for different types like:

<app-grid>
  <ng-template let-rowData="rowData" cellTemplate="student">student {{rowData}}</ng-template>
  <ng-template let-rowData="rowData" cellTemplate="manager">manager {{rowData}}</ng-template>
</app-grid>

Furthermore, ensure that your grid component is equipped to handle specific templates:

grid.component.ts

@ContentChildren(CellTemplateDirective) cellTemplates: QueryList<CellTemplateDirective>;

cellTemplatesMap: { [key: string]: TemplateRef<any> };

...

ngAfterContentInit() {
  this.cellTemplatesMap = this.cellTemplates.reduce((acc, cur) => {
    acc[cur.name] = cur.template;
    return acc;
  }, {});
}

The final step is to include the correct template in your HTML structure:

grid.component.html

<td *ngIf="column.type==='template'">
    <ng-content *ngTemplateOutlet="cellTemplatesMap[column.columnName]; ...

View Forked Stackblitz Example

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Incorrect positioning of AnyChart within a reusable component (Angular version 9.1.3, Bootstrap 4.4.1, Anychart version 8.7.1) causing display issues on the DOM

I have created a test dashboard featuring two Bootstrap cards, each containing an Anychart column chart. The primary objective is to experiment with reusable components. For those interested, here is the code link on Stackblitz Upon running the code, my ...

When using Angular9 with PrimeNG fullcalendar, it may encounter issues such as errors stating "Cannot find namespace 'FullCalendarVDom'." and "Please import the top-level fullcalendar lib"

Using primeng p-fullcalendar in my angular application. Encountering an error in the command-line: 'Cannot find namespace 'FullCalendarVDom' Also seeing this error in the browser: 'Please import the top-level fullcalendar lib before a ...

Avoid navigating to the subscribe block when the server sends a response in Angular

When trying to send a request to the server and check the response, I am not seeing any results. The code for sending the request is below: SendVerificationInfo(item: SendVerificationModel): Observable < any > { return this.httpClient.post < any ...

getStaticProps will not return any data

I'm experiencing an issue with my getStaticProps where only one of the two db queries is returning correct data while the other returns null. What could be causing this problem? const Dash = (props) => { const config = props.config; useEffect(() ...

Having trouble importing React components from a different file?

Having trouble with React due to issues importing components from external files. import React from 'react'; import ReactDOM from 'react-dom'; import '../resources/styles.scss'; import WelcomeView from '../views/welcome/W ...

Is tsconfig.json Utilized by Gatsby When Using Typescript?

Many blog posts and the example on Gatsby JS's website demonstrate the use of a tsconfig.json file alongside the gatsby-plugin-typescript for TypeScript support in Gatsby. However, it seems like the tsconfig.json file is not actually utilized for conf ...

Segment the progress bar into sections

Struggling with an Angular app, and still new to HTML and CSS, I am attempting to create a unique progress bar. I aim to split each section of the progress bar into two subsections with distinct background colors shown in this image: https://i.sstatic.net/ ...

What is the process for transforming every data type within an array?

My problem involves handling various types of data type ParseMustaches<T extends string[], U extends Record<string, string> = {}> = T extends `{{${infer U}}}` ? Record<U, string> : never type Test = ParseMustaches<[" ...

Issue with the loading of Firebase in Chrome extension's background script is inconsistent

I am currently working on developing a Chrome extension that utilizes Google Firebase for authentication. In my project, I am employing webpack for building purposes, with Firebase being utilized in the background script. However, during the initial initi ...

Utilizing the map function in React to achieve similar functionality as *ngFor in Angular

When working in React, we often utilize the map function inside JSX to iterate through items. I'm curious if it's possible to create a custom Repeat component similar to Angular's *ngFor. <Repeat for={[1, 2, 3]}> ... <Repea ...

Troubleshooting error when integrating Power BI with Angular: Module 'powerbi-client-angular' not found or its type declarations

Encountered an error at: import { PowerBIEmbedModule } from "powerbi-client-angular"; error: Cannot find module 'powerbi-client-angular' or its corresponding type declarations `import { NgModule } from '@angular/core';import ...

Issue with Angular UI Bootstrap accordion heading behavior when used in conjunction with a checkbox is causing

I have implemented a checkbox in the header of an accordion control using Bootstrap. However, I am facing an issue where the model only updates the first time the checkbox is clicked. Below is the HTML code for the accordion: <accordion ng-repeat="tim ...

Angular implementation for dynamic attribute of Bootstrap carousel

I have implemented a Bootstrap 5 carousel with the data-bs-ride="carousel" attribute to enable autoplay for slides. However, when I replace "carousel" with "false", the autoplay feature is disabled. My goal is to create buttons that allow the user to paus ...

MAJOR UPDATE: webpack versions before 5 previously contained polyfills for node.js specifically for 'timers-browserify'

Hey there, I'm encountering the error message below: ./node_modules/xml2js/lib/parser.js:38:17-47 - Error: Module not found: Error: Can't resolve 'timers' in '/Users/differentname/Desktop/workfiles/webdoc/ngx-admin-1/node_modules/x ...

The primary text is getting truncated when an ion-note is placed inside an ion-list

I'm currently working with Ionic 3 and attempting to create a list of events where the event name appears on the left and the event note (start time) appears on the right using an ion-note. Below is the code snippet: <ion-list *ngIf="events.len ...

Automatically organize <mat-list-item> elements within a <mat-list> container

Here is the code snippet: <div> <mat-list fxLayout="row" dense> <mat-list-item *ngFor="let label of labelsList"> <!-- just an array of strings --> <button mat-button> ...

Converting Angular HTTP Response from Object of Objects to Array with Interface Typing

After receiving a response, the data is structured as follows: { "0": { "name": "Novartis AG", "symbol": "NVS", "has_intraday": false, "has_eod": true ...

What are the steps to convert this code into Angular syntax?

After realizing that using jQuery alongside Angular is not a good practice, I am determined to modify my code in this Angular App to eliminate the need for jQuery. Currently, I have a page that displays menu items fetched from the server and I loop throug ...

What could be causing the headings and lists to not function properly in tiptap?

I'm currently working on developing a custom text editor using tiptap, but I've encountered an issue with the headings and lists functionalities not working as expected. View the output here 'use client'; import Heading from '@tip ...

What is the best way to assign default values when destructuring interfaces within interfaces in TypeScript?

My goal here is to create a function that can be used with or without arguments. If arguments are provided, it should work with those values; if not, default values should be used. The issue I'm facing is that although there are no TypeScript errors ...