Directives & Pipes Interview Questions
Comprehensive directives & pipes interview questions and answers for Angular. Prepare for your next job interview with expert guidance.
Questions Overview
1. What are directives in Angular and their types?
Basic2. How do you create a custom directive?
Moderate3. What are structural directives and how do they work?
Basic4. What is the difference between pure and impure pipes?
Moderate5. How do you create a custom pipe?
Basic6. What are attribute directives and their use cases?
Basic7. How do you handle events in custom directives?
Moderate8. What are the built-in pipes in Angular?
Basic9. How do you implement directive composition?
Advanced10. What is async pipe and its benefits?
Moderate11. How do you implement custom structural directives?
Advanced12. What are directive lifecycle hooks?
Moderate13. How do you handle pipe parameters?
Basic14. What are host bindings in directives?
Moderate15. How do you implement pipe chaining?
Moderate16. What are exported directives?
Advanced17. How do you handle directive dependencies?
Moderate18. What are the best practices for custom pipes?
Moderate19. How do you test directives and pipes?
Advanced20. What is template context in structural directives?
Advanced21. How do you handle dynamic pipes?
Advanced22. What are directive queries?
Advanced23. How do you handle pipe errors?
Moderate24. What are directive selectors patterns?
Moderate25. How do you implement internationalization in pipes?
Advanced26. What are common directive patterns?
Advanced27. How do you optimize directive performance?
Advanced28. What are directive communication patterns?
Moderate29. How do you handle directive conflicts?
Advanced1. What are directives in Angular and their types?
BasicDirectives are classes that modify elements. Types: Component directives (with template), Structural directives (modify DOM structure like *ngIf), Attribute directives (modify element behavior like [ngStyle]). Core building blocks of Angular.
2. How do you create a custom directive?
ModerateCreate using @Directive decorator. Implement logic in class methods. Use ElementRef/Renderer2 for DOM manipulation. Can have inputs/outputs, host listeners. Consider selector naming, encapsulation.
3. What are structural directives and how do they work?
BasicStructural directives modify DOM structure. Examples: *ngIf, *ngFor, *ngSwitch. Use microsyntax with asterisk (*). Create template views, handle context. Important for dynamic content.
4. What is the difference between pure and impure pipes?
ModeratePure pipes execute only on pure changes (primitive/reference). Impure pipes execute on every change detection. Pure pipes better for performance. Impure needed for dynamic transformations.
5. How do you create a custom pipe?
BasicCreate using @Pipe decorator. Implement PipeTransform interface. Define transform method for conversion logic. Can accept multiple arguments. Consider pure/impure setting.
6. What are attribute directives and their use cases?
BasicAttribute directives change element appearance/behavior. Examples: ngStyle, ngClass. Can respond to user events, modify element properties. Used for dynamic styling, behavior modification.
7. How do you handle events in custom directives?
ModerateEvents handled through @HostListener decorator. Respond to DOM events, custom events. Can access event data, element properties. Consider event propagation, prevention.
8. What are the built-in pipes in Angular?
BasicBuilt-in pipes: DatePipe, UpperCasePipe, LowerCasePipe, CurrencyPipe, DecimalPipe, PercentPipe, AsyncPipe, JsonPipe. Each serves specific transformation need. Consider localization support.
9. How do you implement directive composition?
AdvancedDirective composition through multiple directives on element. Handle directive interaction, priority. Consider order of execution, conflicts. Important for complex behaviors.
10. What is async pipe and its benefits?
ModerateAsyncPipe unwraps observable/promise values. Automatically handles subscription/unsubscription. Updates view on new values. Important for reactive programming, memory management.
11. How do you implement custom structural directives?
AdvancedCustom structural directives use TemplateRef, ViewContainerRef. Create/destroy views programmatically. Handle context binding, view manipulation. Consider microsyntax support.
12. What are directive lifecycle hooks?
ModerateDirective lifecycles: ngOnInit, ngOnDestroy, ngOnChanges, etc. Handle initialization, changes, cleanup. Consider timing, dependency availability. Important for proper resource management.
13. How do you handle pipe parameters?
BasicPipe parameters passed after colon in template. Multiple parameters separated by colons. Access in transform method. Consider optional parameters, default values.
14. What are host bindings in directives?
ModerateHost bindings modify host element properties/attributes. Use @HostBinding decorator. Bind to element properties, classes, styles. Consider property naming, updates.
15. How do you implement pipe chaining?
ModeratePipe chaining connects multiple pipes sequentially. Order matters for transformation. Consider data type compatibility, performance. Use for complex transformations.
16. What are exported directives?
AdvancedExported directives available for template reference. Use exportAs property. Access directive methods/properties in template. Important for directive interaction.
17. How do you handle directive dependencies?
ModerateDirective dependencies injected through constructor. Access services, other directives. Consider dependency scope, availability. Important for directive functionality.
18. What are the best practices for custom pipes?
ModerateBest practices: pure by default, handle null/undefined, proper error handling. Consider performance impact, reusability. Document parameters, behavior.
19. How do you test directives and pipes?
AdvancedTesting through TestBed configuration, component creation. Test directive behavior, pipe transformations. Consider different scenarios, edge cases. Important for reliability.
20. What is template context in structural directives?
AdvancedTemplate context provides data to template. Access through let syntax. Define custom context properties. Important for dynamic content, loops.
21. How do you handle dynamic pipes?
AdvancedDynamic pipes selected at runtime. Use pipe binding syntax. Consider pipe availability, performance. Important for flexible transformations.
22. What are directive queries?
AdvancedDirective queries access other directives. Use ContentChild/ContentChildren, ViewChild/ViewChildren. Access child directives, elements. Consider timing, availability.
23. How do you handle pipe errors?
ModeratePipe errors handled through error handling, default values. Consider null checks, type validation. Provide meaningful error messages. Important for robust applications.
24. What are directive selectors patterns?
ModerateSelector patterns: element, attribute, class selectors. Define directive application scope. Consider naming conventions, specificity. Important for proper directive usage.
25. How do you implement internationalization in pipes?
Advancedi18n in pipes through locale services, formatting options. Support different formats, languages. Consider cultural differences, formats. Important for global applications.
26. What are common directive patterns?
AdvancedCommon patterns: decorator pattern, adapter pattern, composite pattern. Apply based on use case. Consider reusability, maintainability. Important for scalable applications.
27. How do you optimize directive performance?
AdvancedOptimization through proper change detection, minimal DOM manipulation. Use OnPush strategy when possible. Consider event binding, updates frequency. Important for application performance.
28. What are directive communication patterns?
ModerateCommunication through inputs/outputs, services, events. Handle parent-child interaction, sibling communication. Consider component hierarchy, data flow. Important for complex directives.
29. How do you handle directive conflicts?
AdvancedConflicts resolved through priority, specific selectors. Handle multiple directives, compatibility. Consider directive composition, interaction. Important for complex applications.