Angular - Using interpolation with ternary operator in Input property binding

749 Views Asked by At

I am trying to make this property binding to work. I want to use ternary operator where conditions are interpolated strings. HTML is compiling, but the element is not showing.

<si-icon [icon]="item.isSelected ? 'element-face-{{item.faceColor}}-filled' : 'element-face-{{item.faceColor}}'"></si-icon>

Can someone point me what I am doing wrong here? Thank you!

1

There are 1 best solutions below

0
Yong Shun On

Solution 1: In HTML

<si-icon [icon]="
    item.isSelected 
    ? 'element-face-' + item.faceColor + '-filled' 
    : 'element-face-' + item.faceColor">
</si-icon>

Solution 2: Get style name from component.

getIconStyle(isSelected: boolean, faceColor: string): string {
  return isSelected
    ? `element-face-${faceColor}-filled`
    : `element-face-${faceColor}`;
}
<si-icon [icon]="getIconStyle(item.isSelected, item.faceColor)">
</si-icon>

Sample Stackblitz Example