Open In App

Angular PrimeNG Carousel Templates

Angular PrimeNG is an open-source framework with a rich set of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. It provides a lot of templates, components, theme design, an extensive icon library, and much more. In this article, we will see the Angular PrimeNG Carousel Templates.

The Carousel is a content slider component where an array of data is shown sequentially, manually or automatically. The Carousel in Angular PrimeNG provides great customization that can be used to create dynamic carousels. The Carousel Templates are used to place the different components in the Carousel component.



Angular PrimeNG Carousel Templates:

Syntax



<p-carousel [value]="tutorials">
  <ng-template let-item pTemplate="header">
    <h4>...</h4>
  </ng-template>
  <ng-template let-item pTemplate="item">
    <p-image [src]="item.image" alt="..." 
        width="...">
    </p-image>
  </ng-template>
</p-carousel>

Creating Angular application & Module Installation:

Step 1: Create an Angular application using the following command:

ng new geeks_angular

Step 2:  After creating your project folder i.e. geeks_angular, move to it using the following command.

cd geeks_angular

Step 3: Install PrimeNG in your given directory.

npm install primeng --save
npm install primeicons --save

Project Structure: The project structure will look like the following:

 

Steps to run the application: Run the below command to see the output.

ng serve --save
 

Example 1: Below is the example code that illustrates the use of Angular PrimeNG Carousel Templates. In the following example, we have a simple carousel and a header.

app.component.html:




<h1 style="color: green; text-align: center;">
    GeeksforGeeks
</h1>
<h3>Angular PrimeNG Carousel Templates</h3>
<p-carousel [value]="tutorials">
    <ng-template let-item pTemplate="header">
        <h4>Best and free tutorials from GeeksforGeeks</h4>
    </ng-template>
    <ng-template let-item pTemplate="item">
        <p-image [src]="item.image" 
            alt="Image" width="700px">
        </p-image>
    </ng-template>
</p-carousel>

app.component.ts:




import { Component } from "@angular/core";
import { PrimeNGConfig } from "primeng/api";
@Component({
    selector: "app-root",
    templateUrl: "./app.component.html"
})
export class AppComponent {
    tutorials: Tutorial[];
  
    constructor(private primengConfig: PrimeNGConfig) {}
  
    ngOnInit() {
        this.tutorials = [
            {
                title: "Web MH ", image:
            },
            {
                title: "Interview Experience ", image:
            },
            {
                title: "GeeksforGeeks Logo ", image:
            },
            {
                title: "GeeksforGeeks Carnival ", image:
            },
            {
                title: "Python Course ", image:
            }
        ];
    }
}
export interface Tutorial {
    title?: String;
    image?: String;
}

app.module.ts:




import { NgModule } from "@angular/core";
import { BrowserModule } from "@angular/platform-browser";
import { FormsModule } from "@angular/forms";
import { BrowserAnimationsModule } 
    from "@angular/platform-browser/animations";
import { AppComponent } from "./app.component";
import { CarouselModule } from "primeng/carousel";
import { ButtonModule } from "primeng/button";
import { ImageModule } from "primeng/image";
  
@NgModule({
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        CarouselModule,
        ButtonModule,
        FormsModule,
        ImageModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent]
})
  
export class AppModule {}

Output:

 

Example 2: Below is another example code that illustrates the use of Angular PrimeNG Carousel Templates. In the following example, we have a header and a footer with the item template.

app.component.html:




<h1 style="color: green; text-align: center;">
    GeeksforGeeks
</h1>
<h3>Angular PrimeNG Carousel Templates</h3>
<p-carousel [value]="tutorials">
    <ng-template let-item pTemplate="header">
        <h4>Best and free tutorials from GeeksforGeeks</h4>
    </ng-template>
    <ng-template let-item pTemplate="item">
        <p-image [src]="item.image" alt="Image" width="700px">
        </p-image
    </ng-template>
    <ng-template let-item pTemplate="footer">
        <h4>There are {{ tutorials.length }} images</h4>
    </ng-template>
</p-carousel>




import { Component } from "@angular/core";
import { PrimeNGConfig } from "primeng/api";
@Component({
    selector: "app-root",
    templateUrl: "./app.component.html"
})
export class AppComponent {
    tutorials: Tutorial[];
  
    constructor(private primengConfig: PrimeNGConfig) {}
  
    ngOnInit() {
        this.tutorials = [
            {
                title: "Web MH ", image:
            },
            {
                title: "Interview Experience ", image:
            },
            {
                title: "GeeksforGeeks Logo ", image:
            },
            {
                title: "GeeksforGeeks Carnival ", image:
            },
            {
                title: "Python Course ", image:
            }
        ];
    }
}
export interface Tutorial {
    title?: String;
    image?: String;
}

app.module.ts:




import { NgModule } from "@angular/core";
import { BrowserModule } from "@angular/platform-browser";
import { FormsModule } from "@angular/forms";
import { BrowserAnimationsModule } 
    from "@angular/platform-browser/animations";
import { AppComponent } from "./app.component";
import { CarouselModule } from "primeng/carousel";
import { ButtonModule } from "primeng/button";
import { ImageModule } from "primeng/image";
  
@NgModule({
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        CarouselModule,
        ButtonModule,
        FormsModule,
        ImageModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent]
})
  
export class AppModule {}

Output:

 

Reference: http://primefaces.org/primeng/carousel


Article Tags :