Angular PrimeNG Carousel Basic
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. This article will show us how to use Basic Carousel in Angular PrimeNG.
Angular PrimeNG Basic Carousel is used to render the basic carousel. Carousel is a slider type component that offers high customization.
Syntax:
<p-carousel [value]="..">
...
</p-carousel>
Creating Angular application & Module Installation:
Step 1: Create an Angular application using the following command.
ng new appname
Step 2: After creating your project folder i.e. appname, move to it using the following command.
cd appname
Step 3: Install PrimeNG in your given directory.
npm install primeng --save
npm install primeicons --save
Project Structure: It will look like the following:
Project Structure
Example 1: Below is a simple example demonstrating the use of the Angular PrimeNG Basic carousel.
app.component.html
< h2 style = "color: green" >GeeksforGeeks</ h2 >
< h4 >Angular PrimeNG Carousel Basic</ h4 >
< p-carousel [value]="cars">
< ng-template let-car pTemplate = "item" >
< div class = "product-item" >
< div class = "product-item-content" >
< div class = "p-mb-3" ></ div >
< div >
< h4
class = "p-mb-1" >
{{ car.id }}
</ h4 >
< h4
class = "p-mb-1" >
{{ car.name }}
</ h4 >
< h4
class = "p-mb-1" >
{{ car.description }}
</ h4 >
< h6
class = "p-mt-0 p-mb-3" >
${{ car.price }}
</ h6 >
< div class = "car-buttons p-mt-3" >
< p-button
type = "button"
styleClass="p-button
p-button-rounded p-mr-2"
icon = "pi pi-search" >
</ p-button >
< p-button
type = "button"
styleClass="p-button-success
p-button-rounded p-mr-2"
icon = "pi pi-star" >
</ p-button >
< p-button
type = "button"
styleClass="p-button-help
p-button-rounded"
icon = "pi pi-cog" >
</ p-button >
</ div >
</ div >
</ div >
</ div >
</ ng-template >
</ p-carousel >
|
app.component.ts
import { Component } from '@angular/core' ;
interface Car {
id?: string;
name?: string;
description?: string;
price?: number;
}
@Component({
selector: 'app-root' ,
styleUrls: [ './app.component.css' ],
templateUrl: './app.component.html' ,
})
export class AppComponent {
cars: Car[] = [];
constructor() { }
ngOnInit() {
this .cars = [
{
id: '1' ,
name: 'Bugatti' ,
description: 'Racing car' ,
price: 800,
},
{
id: '2' ,
name: 'Ferrari' ,
description: 'The Prancing Horse' ,
price: 1500,
},
{
id: '3' ,
name: 'Porsche' ,
description: 'Full spectrum' ,
price: 10000,
},
];
}
}
|
app.component.css
.product-item-content {
border : 1px solid var(--surface-d);
border-radius: 3px ;
margin : 0.3 rem;
text-align : center ;
padding : 2 rem 0 ;
}
.product-image {
width : 50% ;
box-shadow: 0 3px 6px rgba( 0 , 0 , 0 , 0.16 ),
0 3px 6px rgba( 0 , 0 , 0 , 0.23 );
}
|
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' ;
@NgModule({
imports: [
BrowserModule,
BrowserAnimationsModule,
ButtonModule,
CarouselModule,
FormsModule,
],
declarations: [AppComponent],
bootstrap: [AppComponent],
})
export class AppModule { }
|
Output:
Example 2: Below is another example of a Basic Carousel. In this example, we are using numvisible, numscroll, and circular properties.
app.component.html
< h2 style = "color: green" >GeeksforGeeks</ h2 >
< h4 >Angular PrimeNG Carousel Basic</ h4 >
< p-carousel
[value]="cars"
[numVisible]="2"
[numScroll]="3"
[circular]="false">
< ng-template let-car pTemplate = "itaem" >
< div class = "product-item" >
< div class = "product-item-content" >
< div class = "p-mb-3" ></ div >
< div >
< h4
class = "p-mb-1" >
{{ car.id }}
</ h4 >
< h4
class = "p-mb-1" >
{{ car.name }}
</ h4 >
< h4
class = "p-mb-1" >
{{ car.description }}
</ h4 >
< h6
class = "p-mt-0 p-mb-3" >
${{ car.price }}
</ h6 >
< div class = "car-buttons p-mt-3" >
< p-button
type = "button"
styleClass="p-button
p-button-rounded p-mr-2"
icon = "pi pi-search" >
</ p-button >
< p-button
type = "button"
styleClass="p-button-success
p-button-rounded p-mr-2"
icon = "pi pi-star" >
</ p-button >
< p-button
type = "button"
styleClass="p-button-help
p-button-rounded"
icon = "pi pi-cog" >
</ p-button >
</ div >
</ div >
</ div >
</ div >
</ ng-template >
</ p-carousel >
|
app.component.ts
import { Component } from '@angular/core' ;
interface Car {
id?: string;
name?: string;
description?: string;
price?: number;
}
@Component({
selector: 'app-root' ,
styleUrls: [ './app.component.css' ],
templateUrl: './app.component.html' ,
})
export class AppComponent {
cars: Car[] = [];
constructor() { }
ngOnInit() {
this .cars = [
{
id: '1' ,
name: 'Bugatti' ,
description: 'Racing car' ,
price: 800,
},
{
id: '2' ,
name: 'Ferrari' ,
description: 'The Prancing Horse' ,
price: 1500,
},
{
id: '3' ,
name: 'Porsche' ,
description: 'Full spectrum' ,
price: 10000,
},
];
}
}
|
app.component.css
.product-item-content {
border : 1px solid var(--surface-d);
border-radius: 3px ;
margin : 0.3 rem;
text-align : center ;
padding : 2 rem 0 ;
}
.product-image {
width : 50% ;
box-shadow: 0 3px 6px rgba( 0 , 0 , 0 , 0.16 ),
0 3px 6px rgba( 0 , 0 , 0 , 0.23 );
}
|
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' ;
@NgModule({
imports: [
BrowserModule,
BrowserAnimationsModule,
ButtonModule,
CarouselModule,
FormsModule,
],
declarations: [AppComponent],
bootstrap: [AppComponent],
})
export class AppModule { }
|
Output:
Reference: https://primefaces.org/primeng/carousel
Last Updated :
06 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...