Pass a ng-template as a parameter to a component

When using PrimeNG Table, it allows for the use of body and header templates to customize how the table is rendered. I recently developed a component that encapsulates the PrimeNG table functionality. My question is, how can I successfully pass an ng-template from my component to p-table?

Answer №1

The excellent PrimeNG documentation provides a clear example of this concept. For instance:

<p-table [value]="cars">
    <ng-template pTemplate="header">
        <tr>
            <th *ngFor="let col of cols">
                {{col.header}}
            </th>
        </tr>
    </ng-template>
    <ng-template pTemplate="body" let-car>
        <tr>
            <td *ngFor="let col of cols">
                    {{car[col.field]}}
            </td>
        </tr>
    </ng-template>
</p-table>

This code snippet clearly demonstrates the usage of the body and header templates marked with the pTemplate Directive.
The p-table component can easily locate and utilize these templates through ng-container in combination with the structural Directive ngTemplateOutlet.

If you're interested, you can access the source code here.

@ContentChildren(PrimeTemplate) templates: QueryList<PrimeTemplate>;

Answer №2

One way to pass HTML code to a component is by using ng-content.

Imagine this as creating a custom component:

<div> 
  <ng-content></ng-content>
</div>

Now, let's see how it can be utilized:

<custom-component>
Whatever you write here will be inserted where the ng content is placed.
</custom-component>

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

issue with a xmlhttp request

A JavaScript function using xmlhttp is shown below: var params = "a=" + encodeURIComponent(a) + "&b=" + encodeURIComponent(b) + "&c=" + encodeURIComponent(c); var url = "noob.php"; xmlHttp2.open("POST", url, true); xmlHtt ...

Gathering all the indicated whole numbers from a string and placing them in an array

Upon decoding the URL query using decodeURIComponent and splitting it, the resulting array looks like this: ["pcs_availability:Online", "price:[1500 TO 1999.99]"]. I am trying to extract the proper integers from this array as shown in [1999.99]. In some ca ...

Concealing the presence of jquery and javascript

Currently developing a model-view-controller application (but this question can apply to any website). I'm contemplating whether it's acceptable to have exposed jQuery and JavaScript in a view. Essentially, when the program is run and source code ...

AngularJS rendering causes a hindrance in updating the progress bar

Managing loading objects through ajax is a crucial task. To enhance user experience, I incorporate a progress bar using pNotify and dynamically update it utilizing setInterval before initiating the request. Upon receiving the response, I inject the data o ...

PrimeNG editor: The value is not being displayed in the formControl

I am working with a component that includes a formGroup containing two controls: `enFormGroup: FormGroup = this.fb.group({ titleEn: ['test', Validators.required], textEn: ['hello world', Validators.required], });` Withi ...

Adding elements to the <Head> section in Next.js

I am facing an issue where I need to change the page title dynamically based on the route of the web pages. I have been assigned the task of creating and importing a title component into the layout file, but despite my efforts, nothing seems to change. con ...

What is the proper way to declare static references in the Composition API with Typescript?

Currently, I am using the Composition API (with <script setup lang='ts'>) to create a ref that is utilized in my template: const searchRef = ref(null) onMounted(() => { searchRef.value.focus() }) Although it works and my code compiles w ...

Having trouble running react-native on iOS?

I am currently following the lesson on this website Versions used: npm 5.4.2 homebrew 1.3.3 react-native-cli: 2.0.1 react-native: 0.48.4 watchman 4.9.0 node v6.6.0 The commands I have executed are: admin@admin  ~/Documents/ReactNativeLear ...

Startling error: Our node server.js running into an unexpected token

I'm attempting to follow the Angular Universal quickstart, but I encountered an error when running "node server.js". Emily's-MBP:vepo Emily$ node server.js /Users/Emily/Development/vepo/server.js:3 import 'angular2-universal/polyfills&apos ...

Adjusting Text Width in fabric.js: A How-To Guide

In my experience, I have found that setting textAlign for a fabric.js Text object seems to be ineffective because the textfield's width is automatically adjusted to fit the text perfectly. I searched through the API but couldn't find any way to ...

The Angular 11 library module has been successfully imported into the consuming app but it is not being utilized

Currently, I am in the process of creating an Angular library that will encompass services, pipes, and directives to be utilized across various Angular projects within my organization. At this point, I have successfully implemented three services within th ...

JavaScript embedded in an HTML document, which in turn is embedded within JavaScript

Is it possible to nest tags within other tags to control the functionality of a download button in a chat bot? Unfortunately, nesting tags is not allowed, so I'm looking for an alternative solution. Below is the complete HTML file I'm working wit ...

Troubleshooting Angular testing using Jest: Problem encountered with triggerEventHandler

I'm currently working on an Angular application and testing it with JEST. Everything seems to be running smoothly until I encounter an issue with event interactions in Angular. The test below works as expected: it('should delegate click to comp ...

Implementing Winston logging into a React JS project

I am looking to integrate Winston logging into my React application that was created using create-react-app. First, I installed Winston by running the following command: npm install winston Next, I imported Winston into my app.js file like so: import win ...

The title tag is missing in the head section of a Next.js 13.4 application

I'm currently working on a project using Next.js version 13.4. I have included a title and description within the metadata, but for some reason, it is not showing up on the browser. "use client"; import Navbar from "@/components/Navbar& ...

Generating variables programmatically from an array within the Ajax success callback

Hi there, I am currently working with an AJAX call and have the following code: $.each(data.my_json, function(i, item) { const op_id = item.op_id; const op_name = item.op_name; let nv_tmp_totals = item.nv_tmp_totals; let nvc_tmp_totals = ...

Ways to deactivate just the Clicked button in a mapped over component

Utilizing the request variable, I am displaying an array of objects on the page. Each object in the array contains properties such as question, correct_answer, and incorrect_answer. Moreover, I have implemented a state called disable which toggles between ...

Ways to notify users about batch progress updates in a Spring Boot and Angular web application

My application features a Spring Boot back-end and an Angular front-end that communicates with the back-end through REST endpoints. I am looking to enable users to initiate a batch process on the server, which involves executing multiple API calls to exte ...

Using Google OAuth2Client with Angular 4

I am encountering an issue while trying to verify the ID token for my client using Google's example. You can find the example code here. const {OAuth2Client} = require('google-auth-library'); // <-- facing issues here const client = new ...

Incorporating interactive maps into an AngularJS web application

I've been attempting to integrate Google Maps into my AngularJS application, using the script tag below: <script src="https://maps.googleapis.com/maps/api/js?key=[MySecretKeyHere]&callback=initMap" async defer></script> I found this ...