简体   繁体   English

如何从下拉 angular prime ng 为元素设置其他模板

[英]how set other templates for elements from dropdown angular prime ng

I have a dropdown from PrimeNg with few elements.我有一个来自 PrimeNg 的下拉列表,其中包含很少的元素。

.ts .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 .html

<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 .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我设法使用 ng-templates 为所有元素设置背景,但我不知道如何为下拉列表中的每个元素制作不同的 colors

link to the code 链接到代码

add styleClass Attribute in your array:在您的数组中添加 styleClass 属性:

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并在 HTML

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM