📖 Scenario: You are building a simple Angular component that shows a list of fruits. You want to learn how to use structural directives to control what is shown and attribute directives to style elements.
🎯 Goal: Create an Angular component that uses a structural directive *ngIf to show a message when the fruit list is empty, and an attribute directive [style.color] to color the fruit names.
📋 What You'll Learn
Create a component with a list of fruits
Add a boolean variable to control if the list is empty
Use *ngIf to show a message when the list is empty
Use an attribute directive to color the fruit names in green
💡 Why This Matters
🌍 Real World
Structural directives like *ngIf and *ngFor help show or hide parts of the page or repeat elements. Attribute directives change how elements look or behave. These are common in Angular apps to build dynamic user interfaces.
💼 Career
Understanding structural and attribute directives is essential for Angular developers to create interactive and well-styled web applications.
Progress0 / 4 steps
1
Set up the fruit list
Create a component property called fruits and set it to the array ["Apple", "Banana", "Cherry"].
Angular
Hint
Use fruits = ["Apple", "Banana", "Cherry"] inside your component class.
2
Add a boolean to control empty list
Add a boolean property called isEmpty and set it to false.
Angular
Hint
Use isEmpty = false inside your component class.
3
Use *ngIf to show empty message
In the component template, use the structural directive *ngIf="isEmpty" on a <p> tag to show the text "No fruits available" when isEmpty is true.
Angular
Hint
Use <p *ngIf="isEmpty">No fruits available</p> in your template.
4
Use attribute directive to color fruit names
In the template, use *ngFor="let fruit of fruits" on a <li> tag to list each fruit. Add an attribute directive [style.color]="'green'" to color the fruit names green.
Angular
Hint
Use <li *ngFor="let fruit of fruits" [style.color]="'green'">{{ fruit }}</li> inside a <ul>.
Practice
(1/5)
1. Which of the following best describes a structural directive in Angular?
easy
A. It binds data to an element's property.
B. It changes the style of an element without altering the DOM structure.
C. It changes the layout by adding or removing elements from the DOM.
D. It listens to user events like clicks or key presses.
Solution
Step 1: Understand what structural directives do
Structural directives modify the DOM layout by adding or removing elements dynamically.
Step 2: Compare with other directive types
Attribute directives change appearance or behavior but do not add or remove elements.
Final Answer:
It changes the layout by adding or removing elements from the DOM. -> Option C
Thinking *ngIf and attribute bindings cannot be combined
Confusing *ngIf with [ngIf]
Replacing attribute bindings with structural directive syntax
5. You want to create a custom directive that highlights text by changing its background color when hovered. Which type of directive should you create and why?
hard
A. Structural directive, because it needs to add or remove elements on hover.
B. Attribute directive, because it changes the element's appearance without altering layout.
C. Structural directive, because it modifies the DOM structure dynamically.
D. Attribute directive, because it listens to user events but does not change styles.
Solution
Step 1: Understand the directive's purpose
The directive changes background color on hover, affecting appearance only.
Step 2: Choose directive type based on behavior
Attribute directives change element style or behavior without adding/removing elements, fitting this case.
Final Answer:
Attribute directive, because it changes the element's appearance without altering layout. -> Option B
Quick Check:
Appearance change = attribute directive [OK]
Hint: Appearance changes use attribute directives, layout changes use structural [OK]
Common Mistakes:
Choosing structural directive for style changes
Confusing event listening with directive type
Thinking attribute directives add/remove elements
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.