Bird
0
0

Why does this code cause an error?

medium📝 Debug Q7 of 15
Angular - Directives
Why does this code cause an error?
color = 'blue';
<div [ngStyle]="{color}">Text</div>
AThe variable 'color' is not defined in the component
BObject shorthand syntax is invalid in Angular templates
CAngular requires string keys in ngStyle object
DMissing quotes around the key 'color' in the object
Step-by-Step Solution
Solution:
  1. Step 1: Understand object syntax in Angular templates

    Angular requires keys in the ngStyle object to be strings, so keys must be quoted.

  2. Step 2: Analyze the provided code

    The key color is unquoted, causing a syntax error.

  3. Final Answer:

    Missing quotes around the key 'color' in the object -> Option D
  4. Quick Check:

    Keys in ngStyle object must be quoted strings [OK]
Quick Trick: Always quote keys in ngStyle style object [OK]
Common Mistakes:
  • Using object shorthand without quotes
  • Assuming Angular accepts unquoted keys
  • Confusing variable names with keys

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes