Building Reusable Behaviors
Package behavior as composable directives.
Building Reusable Behaviors is a free Angular Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Angular Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Behaviors as Building Blocks
The composition API shines when you design small, focused directives, each encapsulating one behavior, then compose them onto components as needed.
A Single-Purpose Directive
Keep each behavior tiny and standalone.
@Directive({
selector: '[appElevate]'
})
export class ElevateDirective {
@HostBinding('style.boxShadow')
shadow = '0 2px 8px rgba(0,0,0,.15)';
}A Hover Behavior
Another behavior listens for hover and toggles a class.
@Directive({ selector: '[appHoverClass]' })
export class HoverClassDirective {
@HostBinding('class.is-hover') hovering = false;
@HostListener('mouseenter') on() { this.hovering = true; }
@HostListener('mouseleave') off() { this.hovering = false; }
}Composing Behaviors
A card component composes both behaviors with no template wiring.
@Component({
selector: 'app-card',
hostDirectives: [ElevateDirective, HoverClassDirective],
template: '<ng-content></ng-content>'
})
export class CardComponent {}Reuse Across Components
The same behaviors can be composed onto buttons, tiles, or panels. Define once, reuse everywhere, no duplication.
Configurable Behaviors
Add inputs to make a behavior tunable, then expose them where composed.
@Directive({ selector: '[appElevate]' })
export class ElevateDirective {
level = input(1);
}Exposing the Knob
Expose the configurable input so each host can set it.
hostDirectives: [{
directive: ElevateDirective,
inputs: ['level']
}]Coordinating Behaviors
Behaviors can inject each other through the element injector to coordinate, for example a ripple reacting to a pressed state.
Testability
Small behaviors are easy to unit test in isolation, then trusted when composed, improving overall reliability.
Naming Conventions
Name behaviors after what they do (appElevate, appHoverClass, appAutofocus). Clear names make composition self-documenting.
A Behavior Library
Over time you build a library of composable behaviors that any component can opt into, dramatically reducing repeated host logic.
Quick Check
Test your understanding of reusable behaviors.
Recap
You learned to design small standalone directives that each encapsulate one behavior, compose them with hostDirectives, expose tunable inputs, and build a reusable, testable behavior library.
Frequently asked questions
Is the “Building Reusable Behaviors” lesson free?
Yes — the full text of “Building Reusable Behaviors” is free to read here on the web, and the Angular Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Angular Academy course, upgrade to CoddyKit PRO.
What will I learn in “Building Reusable Behaviors”?
Package behavior as composable directives. You practise Angular Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Angular Academy?
No prior experience is required. Angular Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Building Reusable Behaviors” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Angular Academy lesson?
Yes. Every Angular Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- The hostDirectives Property
- Exposing Inputs and Outputs
- Building Reusable Behaviors
- Composition vs Inheritance