Bird
0
0

You want to create a custom directive that highlights text by changing its background color when hovered. Which type of directive should you create and why?

hard📝 Application Q15 of 15
Angular - Directives
You want to create a custom directive that highlights text by changing its background color when hovered. Which type of directive should you create and why?
AStructural directive, because it needs to add or remove elements on hover.
BAttribute directive, because it changes the element's appearance without altering layout.
CStructural directive, because it modifies the DOM structure dynamically.
DAttribute directive, because it listens to user events but does not change styles.
Step-by-Step Solution
Solution:
  1. Step 1: Understand the directive's purpose

    The directive changes background color on hover, affecting appearance only.
  2. Step 2: Choose directive type based on behavior

    Attribute directives change element style or behavior without adding/removing elements, fitting this case.
  3. Final Answer:

    Attribute directive, because it changes the element's appearance without altering layout. -> Option B
  4. Quick Check:

    Appearance change = attribute directive [OK]
Quick Trick: Appearance changes use attribute directives, layout changes use structural [OK]
Common Mistakes:
  • Choosing structural directive for style changes
  • Confusing event listening with directive type
  • Thinking attribute directives add/remove elements

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes