0
0
Angularframework~5 mins

ngStyle for dynamic styles in Angular - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is ngStyle used for in Angular?

ngStyle is a directive that lets you set CSS styles dynamically on an HTML element based on component data.

Click to reveal answer
beginner
How do you bind multiple styles using ngStyle?

You pass an object where keys are CSS property names and values are style values, like {'color': 'red', 'font-weight': 'bold'}.

Click to reveal answer
beginner
Can ngStyle update styles reactively when component data changes?

Yes, ngStyle updates the styles automatically whenever the bound data changes, reflecting the new styles instantly.

Click to reveal answer
beginner
Show an example of using ngStyle to change text color based on a component property isError.
<div [ngStyle]="{'color': isError ? 'red' : 'green'}">Status message</div>
Click to reveal answer
intermediate
What is the difference between ngStyle and inline style binding like [style.color]?

ngStyle can set multiple styles at once using an object, while [style.color] binds only one style property.

Click to reveal answer
What type of value does ngStyle expect for binding?
AAn object with CSS property names and values
BA string with CSS styles
CA boolean value
DAn array of style names
How does ngStyle react when the bound data changes?
AIt does not change styles
BIt requires manual refresh
CIt updates the styles automatically
DIt throws an error
Which syntax correctly applies ngStyle to set background color to blue?
A[style.backgroundColor]="'blue'"
B[ngStyle]="'background-color: blue'"
Cstyle="background-color: blue"
D[ngStyle]="{'background-color': 'blue'}"
Which is a benefit of using ngStyle over inline styles?
AAlways faster rendering
BAllows dynamic style changes based on component data
CWorks only with static styles
DRequires no Angular knowledge
If you want to set multiple styles conditionally, which Angular directive is best?
AngStyle
BngClass
CngIf
DngFor
Explain how ngStyle helps in applying dynamic styles in Angular components.
Think about how styles can change when data changes.
You got /3 concepts.
    Describe the difference between using ngStyle and binding individual style properties like [style.color].
    Consider how many styles you want to change at once.
    You got /3 concepts.