Open In App

Angular PrimeNG Form Dropdown Properties Component

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 the Form Dropdown Properties Component in Angular PrimeNG.

The Properties Component is used to provide different properties to modify the dropdown.



Properties Used:

 



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: 

 

Example1: In the below code, we will be using the above syntax to demonstrate the use of the Form Dropdown Properties Component.




<div style="text-align:center;">
    <h1 style="color:green;">GeeksforGeeks</h1>
    <h3>A computer science portal for geeks</h3>
    <h4>Angular PrimeNG Form Dropdown Properties Component</h4>
  
    <h5>Single</h5>
    <p-dropdown
        [options]="cities"
        placeholder="Select a City"
        optionLabel="name"
        [showClear]="true">
      </p-dropdown>
</div>




import { Component } from '@angular/core';
  
interface City {
    name: string,
    code: string
}
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.scss']
})
  
export class AppComponent { 
    cities: City[];
  
    constructor() {
        this.cities = [
            {name: 'New York', code: 'NY'},
            {name: 'Rome', code: 'RM'},
            {name: 'London', code: 'LDN'},
            {name: 'Istanbul', code: 'IST'},
            {name: 'Paris', code: 'PRS'}
        ];
    }
}




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 { DropdownModule } from 'primeng/dropdown';
  
@NgModule({
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        DropdownModule,
        FormsModule
    ],
    declarations: [ AppComponent ],
    bootstrap:    [ AppComponent ]
})
  
export class AppModule { }




:host ::ng-deep .p-dropdown {
    width: 14rem;
}

Output:

 

Example2: Below is another example code, that demonstrates the use of the Form Dropdown Properties Component.




<div style="text-align:center;">
    <h1 style="color:green;">GeeksforGeeks</h1>
    <h3>A computer science portal for geeks</h3>
    <h4>Angular PrimeNG Form Dropdown Properties Component</h4>
  
    <h5>Group</h5>
    <p-dropdown
        [options]="gfgGropedCities"
        placeholder="Select a City"
        [group]="true">
        <ng-template let-group pTemplate="group">
            <div class="p-d-flex p-ai-center">
                <span>{{ group.label }}</span>
            </div>
        </ng-template>
    </p-dropdown>
</div>




import { Component } from '@angular/core';
import {SelectItemGroup} from 'primeng/api';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.scss']
})
  
export class AppComponent { 
    gfgGropedCities: SelectItemGroup[];
  
    constructor() {
  
        this.gfgGropedCities = [
            {
                label: 'Germany', value: 'de'
                items: [
                    {label: 'Berlin', value: 'Berlin'},
                    {label: 'Frankfurt', value: 'Frankfurt'},
                    {label: 'Hamburg', value: 'Hamburg'},
                    {label: 'Munich', value: 'Munich'}
                ]
            },
            {
                label: 'USA', value: 'us'
                items: [
                    {label: 'Chicago', value: 'Chicago'},
                    {label: 'Los Angeles', value: 'Los Angeles'},
                    {label: 'New York', value: 'New York'},
                    {label: 'San Francisco', value: 'San Francisco'}
                ]
            },
            {
                label: 'Japan', value: 'jp'
                items: [
                    {label: 'Kyoto', value: 'Kyoto'},
                    {label: 'Osaka', value: 'Osaka'},
                    {label: 'Tokyo', value: 'Tokyo'},
                    {label: 'Yokohama', value: 'Yokohama'}
                ]
            }
        ];
    }
}




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 { DropdownModule } from 'primeng/dropdown';
  
@NgModule({
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        DropdownModule,
        FormsModule
    ],
    declarations: [ AppComponent ],
    bootstrap:    [ AppComponent ]
})
  
export class AppModule {}




:host ::ng-deep .p-dropdown {
    width: 14rem;
}

Output:

 

Reference: https://www.primefaces.org/primeng/dropdown


Article Tags :