Directive Composition API vs. Inheritance
CONCEPTS:Directive Composition API
Question Variations
- "Why is the Directive Composition API preferred over component inheritance in modern Angular?"
- "How do you use `hostDirectives` to share functionality between components?"
- "Can you map inputs and outputs from a host directive to the host component? How?"
- "What are the limitations of the Directive Composition API compared to traditional inheritance?"
Why This Is Asked
Logic reuse is a classic software engineering challenge. In the past, Angular developers often turned to component inheritance, which is brittle and complex. The Directive Composition API is the modern, preferred way to share UI logic. Interviewers ask this to see if you understand how to build flexible, composable UI components and if you’re aware of the latest architectural best practices introduced in Angular 15+.
Key Concepts
- Composition over Inheritance: Why inheritance is an anti-pattern in modern UI frameworks.
- Host Directives: Applying directives to a component’s host via metadata.
- Input/Output Mapping: Exposing directive properties through the host component.
- Shared Logic: Using directives for common behaviors like
Tooltip,Ripple, orValidation.
Question Variations
- “Why is the Directive Composition API preferred over component inheritance in modern Angular?”
- “How do you use
hostDirectivesto share functionality between components?” - “Can you map inputs and outputs from a host directive to the host component? How?”
- “What are the limitations of the Directive Composition API compared to traditional inheritance?”