How to add multiple if statements in a div

I wanna add multiple conditions in one div in order to change the color dynamically like this:

<div tappable  [style.background]= "if x=1 let color be red ; if x=2 let color be green ;if x=3 let color be blue">
      <div>
      </div>
       ......
    </div>

I know how to use it for two conditions with the “?”. but I am new to ionic and could not apply multi conditions.

Hello,
maybe it is easier with

 [style.background]= "myvariable"

and in your .ts file you set our variable like you want.

switch(x){
case 1:
myvariable = 'red';
break;
case 2:
myvariable = 'green';
break;
default:
throw 'x ist nix';
}

Maybe this helps: https://scotch.io/tutorials/angular-2-classes-with-ngclass-and-ngstyle

Best regards, anna-liebt

Look at ngStyle and ngClass.

1 Like