如何使用Angular 4从当前选定的选项中设置选择的背景颜色

时间:2017-12-12 01:09:03

标签: html angular

我尝试根据当前所选的包含其颜色的选项设置选择的背景颜色。我一直坚持如何设置样式(我使用Angular 4)。

我尝试做的一个例子是:

<select class="form-control" [ngStyle]="this.options[this.selectedIndex].getAttribute('style')"
                        [(ngModel)]="result.Id"
                        name="test"
                        required>
                    <option *ngFor="let weight of riskWeights" [ngStyle]="{'background-color': weight.color}" [ngValue]="weight.Id">{{weight.name}}</option>
                </select>

我出错的地方是尝试获取当前所选项目并设置样式:[ngStyle] =&#34; this.options [this.selectedIndex] .getAttribute(&#39; style&#39 ;)&#34 ;.我无法正确读取当前所选项目的背景颜色并将其应用于选择。

关于如何做到这一点的任何想法?

1 个答案:

答案 0 :(得分:1)

您正尝试设置List的背景而不是List asList(Object[])

您可以使用选项的值设置选择的颜色,如下所示,

option

Example