Built-in
Structural directives are responsible for HTML layout. They shape or reshape the DOM's structure, typically by adding, removing, and manipulating the host elements to which they are attached.
This section introduces the most common built-in structural directives:
NgIf
1. Component TypeScript:
Define the property and method to toggle the visibility of the message.
TypeScript (Component):
import { Component } from '@angular/core';
@Component({
selector: 'app-ngif-example',
templateUrl: './ngif-example.component.html',
styleUrls: ['./ngif-example.component.css']
})
export class NgIfExampleComponent {
// Property to control the visibility of the message
isMessageVisible: boolean = false;
// Method to toggle the message visibility
toggleMessage() {
this.isMessageVisible = !this.isMessageVisible;
}
}2. Component HTML:
Use *ngIf to conditionally display the message based on the isMessageVisible property. Add a button to toggle this property.
HTML:
3. Component CSS (Optional):
You can include some CSS for styling, but it is not necessary for this example.
CSS:
Result:
Initial State: The message is not displayed because
isMessageVisibleis initially set tofalse.
Rendered HTML:
Initially:
Button Clicks: Clicking the button will toggle the
isMessageVisibleproperty. WhenisMessageVisibleistrue, the message will be shown; whenisMessageVisibleisfalse, the message will be hidden.
After Interaction:
After clicking "Show Message":
Clicking "Hide Message" will hide the message again.
Summary
*ngIfconditionally includes or excludes elements from the DOM based on a boolean condition.The button toggles the value of
isMessageVisible, demonstrating how*ngIfcan be used to manage element visibility dynamically.User interactions with the button update the component state and affect the display of the message.
NgFor
1. Component TypeScript:
Define the list of items and a method to add a new item.
TypeScript (Component):
2. Component HTML:
Use *ngFor to iterate over the list of items and display each item. Include an input field and button to add new items.
HTML:
3. Component CSS (Optional):
You can include some CSS for styling, but it’s not essential for this example.
CSS:
Result:
Initial State: The list will display the initial items: "Apple", "Banana", and "Cherry".
Rendered HTML:
Initially:
Adding Items: Typing a new item in the input field and clicking "Add Item" will add the new item to the list.
After Interaction:
For example, after typing "Orange" and clicking "Add Item":
Summary
*ngForis used to iterate over an array or collection and create a template for each item.The input field and button allow users to add new items dynamically to the list.
User interactions with the input and button show how
*ngForcan dynamically update the view based on the component’s state.
NgSwitch
1. Component TypeScript:
Define the currentDay property and options for the days of the week.
TypeScript (Component):
2. Component HTML:
Use *ngSwitch and *ngSwitchCase to conditionally display content based on the value of currentDay.
HTML:
3. Component CSS (Optional):
You can include some CSS for styling, but it’s not necessary for this example.
CSS:
Result:
Initial State: The message corresponding to the initial value of
currentDay(e.g.,'Monday') will be displayed.
Rendered HTML:
Initially (with currentDay set to 'Monday'):
Button Clicks: Clicking a button will update the
currentDayproperty, and the content inside thengSwitchcontainer will change based on the new value.
After Interaction:
For example, after clicking the "Friday" button:
Summary
*ngSwitchis used to conditionally display content based on a value.*ngSwitchCasespecifies the content to display for each possible value.*ngSwitchDefaultprovides fallback content when no cases match.User interactions with the buttons demonstrate how
*ngSwitchdynamically updates the view based on thecurrentDayvalue.
Last updated