我尝试根据当前所选的包含其颜色的选项设置选择的背景颜色。我一直坚持如何设置样式(我使用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 ;.我无法正确读取当前所选项目的背景颜色并将其应用于选择。
关于如何做到这一点的任何想法?