Why directives are needed in Angular
📖 Scenario: You are building a simple Angular app to display a list of tasks. You want to reuse a piece of HTML code that highlights tasks that are completed. Instead of repeating the same code everywhere, you will create a directive to add this highlight behavior.
🎯 Goal: Build an Angular directive that changes the background color of a task item when it is marked as completed. This shows why directives are needed to add reusable behavior to HTML elements.
📋 What You'll Learn
Create a standalone Angular directive named
HighlightCompletedDirectiveUse an input property named
appHighlightCompleted to receive a boolean indicating if the task is completedChange the background color of the host element to lightgreen if the task is completed
Keep the background color transparent if the task is not completed
Use the directive in a component template to highlight completed tasks
💡 Why This Matters
🌍 Real World
Directives are used in Angular apps to add custom behavior to elements, such as highlighting, validation, or animations, without repeating code.
💼 Career
Understanding directives is essential for Angular developers to build clean, reusable, and maintainable UI components.
Progress0 / 4 steps