Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how set other templates for elements from dropdown angular prime ng

I have a dropdown from PrimeNg with few elements.

.ts

this.cities = [
        {name: 'New York', code: 'NY'},
        {name: 'Rome', code: 'RM'},
        {name: 'London', code: 'LDN'},
        {name: 'Istanbul', code: 'IST'},
        {name: 'Paris', code: 'PRS'}
    ];

.html

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<p-dropdown 
[options]="cities" 
[(ngModel)]="selectedCity1" 
placeholder="Select a City" 
optionLabel="name"
[showClear]="true">
<ng-template let-item pTemplate="item" >
<span style="background-color: green">{{item.name}}</span>
</ng-template>
</p-dropdown>

.scss

.green {
  background-color: green;
  }
  
.blue {
  background-color: blue;
}

.red {
  background-color: red;
}

.yellow {
  background-color: yellow;
}

.pink {
  background-color: pink;
}

I managed to set the background for all the elements using ng-templates, but I can’t figure out how to make different colors for each element from the drop-down list

link to the code

>Solution :

add styleClass Attribute in your array:

this.cities = [
    {name: 'New York', code: 'NY', styleClass: 'green'},
    {name: 'Rome', code: 'RM', styleClass: 'blue'},
    {name: 'London', code: 'LDN', styleClass: 'red'},
    {name: 'Istanbul', code: 'IST', styleClass: 'yellow'},
    {name: 'Paris', code: 'PRS', styleClass: 'pink'}
];

and in the HTML

<span [class]="item.styleClass">{{item.name}}</span>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading