Bird
0
0

Given this directive that changes background color:

medium📝 component behavior Q4 of 15
Angular - Directives
Given this directive that changes background color:
@Directive({selector: '[appBgColor]'})
export class BgColorDirective {
  constructor(el: ElementRef) {
    el.nativeElement.style.backgroundColor = 'yellow';
  }
}

What will be the background color of this element?
<div appBgColor>Hello</div>
ABlue
BYellow
CNo color change
DRed
Step-by-Step Solution
Solution:
  1. Step 1: Understand directive effect on element

    The directive sets the element's background color to yellow in the constructor.
  2. Step 2: Apply directive to the div element

    The div with appBgColor will have its background color changed to yellow.
  3. Final Answer:

    Yellow -> Option B
  4. Quick Check:

    Directive sets background color = Yellow [OK]
Quick Trick: Directive constructor changes element style immediately [OK]
Common Mistakes:
  • Assuming no style change without explicit binding
  • Confusing directive selector with component selector
  • Expecting default color instead of yellow

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes