Discover how a simple directive can save you hours of repetitive coding!
Why directives are needed in Angular - The Real Reasons
Imagine you want to add the same interactive behavior, like a tooltip or a dropdown menu, to many parts of your webpage by writing the same JavaScript code over and over again.
Manually repeating code for each element is tiring, easy to forget, and makes your page slow and messy. It's hard to keep track of all changes and fix bugs when the same code is everywhere.
Angular directives let you package behavior into reusable tags or attributes. You write the code once, then just add the directive wherever you want that behavior, keeping your code clean and easy to manage.
const buttons = document.querySelectorAll('.tooltip'); buttons.forEach(btn => { btn.addEventListener('mouseenter', () => showTooltip(btn)); btn.addEventListener('mouseleave', () => hideTooltip(btn)); });
<button appTooltip="Info">Hover me</button>Directives make it simple to add consistent, reusable behaviors across your app without repeating code.
Think of a website where every button shows a helpful tooltip on hover. Instead of writing tooltip code for each button, you just add a directive to each one.
Manual repetition of behavior is slow and error-prone.
Directives package behavior for easy reuse.
This keeps your code clean, consistent, and easier to maintain.