I am using Angular2, I would like to set the style dinamically using property names in component. Following is what I have tried, but it is not working.
<span class = "label" [style.background-color]="property1+'_css'">{{property1}} </span> <span class = "label" [style.background-color]="property2+'_css'">{{property2}} </span> export class ABC implements OnInit, OnChanges { property1 : string = "#f89406"; property2 : string = "#3a87ad"; } If someone knows about it, could you give some advice?
I rephrazed my words, I wanted to change background color according to item name like codes below. I have properties like below in the component. item1, item2 and item3 points to {{item.name}} in the template. It is fine to code statistically css name according to item name. But I do want to suffix with _css for those properties. So, I put '_css' at the end. Also, right to the [style.background-color] is not the property defined directly in the component, it is variable defined in the ngFor of template.
<tr *ngFor="let item of items"> <b><span class = "label" [style.background-color]="item.name+'_css'">{{item.name}}</span></b> </tr> export class ABC implements OnInit, OnChanges { item1_css : string = "#f89406"; item2_css : string = "#3a87ad"; item3_css : string = "#2283c5"; }
'_css'part?thisis not allowed in the template AFAIK. You can trythisinstead ofselfin the view and ignore the getter.<tr>can't contain a<b>element? only an<td>