Angular - How to apply [ngStyle] conditions

后端 未结 4 929
孤街浪徒
孤街浪徒 2021-02-01 01:00

I have a div that I want to style based on a condition.

If styleOne is true I want a background colour of red. If StyleTwo is true, I want the background colour to be blu

4条回答
  •  有刺的猬
    2021-02-01 02:01

    You can use an inline if inside your ngStyle:

    [ngStyle]="styleOne?{'background-color': 'red'} : {'background-color': 'blue'}"
    

    A batter way in my opinion is to store your background color inside a variable and then set the background-color as the variable value:

    [style.background-color]="myColorVaraible"
    

提交回复
热议问题