0
0
Angularframework~20 mins

ngStyle for dynamic styles in Angular - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
ngStyle Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What style will be applied by ngStyle?
Given the Angular component template below, what will be the background color of the <div> when isActive is true?
Angular
<div [ngStyle]="{'background-color': isActive ? 'green' : 'red'}">Status Box</div>
AThe background color will be green.
BThe background color will be red.
CThe background color will be blue.
DNo background color will be applied.
Attempts:
2 left
💡 Hint
Check the ternary condition inside ngStyle for the background-color property.
📝 Syntax
intermediate
2:00remaining
Identify the correct ngStyle syntax
Which of the following is the correct syntax to apply multiple dynamic styles using ngStyle in Angular?
A<div ngStyle="color: textColor; font-size: fontSize + 'px'"></div>
B<div [ngStyle]="{'color': textColor, 'font-size': fontSize + 'px'}"></div>
C<div [ngStyle]="color: textColor; font-size: fontSize + 'px'"></div>
D<div [ngStyle]="{color: 'textColor', fontSize: fontSize + 'px'}"></div>
Attempts:
2 left
💡 Hint
Remember that ngStyle expects an object with style properties as keys and values as expressions.
🔧 Debug
advanced
2:00remaining
Why does ngStyle not update dynamically?
Consider this Angular component snippet:
styles = { 'color': 'blue' };

updateColor() {
  this.styles.color = 'red';
}

And the template:
<div [ngStyle]="styles">Text</div>

Why might the color not change to red after calling updateColor()?
ABecause the <code>updateColor</code> method is missing a return statement.
BBecause ngStyle only accepts string literals, not object variables.
CBecause Angular does not detect changes when object properties are mutated directly.
DBecause the styles object must be reassigned to a new object for change detection.
Attempts:
2 left
💡 Hint
Think about how Angular detects changes in objects passed to bindings.
state_output
advanced
2:00remaining
What styles are applied after state change?
Given this Angular component code:
isHighlighted = false;

getStyle() {
  return {
    'font-weight': this.isHighlighted ? 'bold' : 'normal',
    'color': this.isHighlighted ? 'orange' : 'black'
  };
}

And template:
<button (click)="isHighlighted = !isHighlighted">Toggle</button>
<span [ngStyle]="getStyle()">Text</span>

What will be the style of the <span> after clicking the button once?
Afont-weight: normal; color: black;
Bfont-weight: normal; color: orange;
Cfont-weight: bold; color: black;
Dfont-weight: bold; color: orange;
Attempts:
2 left
💡 Hint
The button toggles the boolean, changing styles accordingly.
🧠 Conceptual
expert
2:00remaining
Why prefer ngStyle over inline style bindings?
In Angular, why is it generally better to use ngStyle for dynamic styling instead of directly binding to the style attribute like [style.color] or inline styles?
ABecause ngStyle allows setting multiple styles at once with a single binding, improving readability and performance.
BBecause [style.color] bindings are deprecated and no longer supported in Angular 17+.
CBecause ngStyle prevents any style changes unless explicitly triggered by user events.
DBecause ngStyle automatically adds vendor prefixes to CSS properties.
Attempts:
2 left
💡 Hint
Think about managing multiple styles and code clarity.