Skip to main content

8 docs tagged with "structural directives"

View All Tags

ng-content

`` in Angular is a special directive used in component templates to create a content projection spot. It allows you to compose complex components by including content from outside the component. This feature is particularly useful for creating reusable and customizable components.

ngClass

ngClass is a built-in directive in Angular that allows you to dynamically add or remove CSS classes to an HTML element based on an expression. This directive is particularly useful for changing the appearance of elements in response to component state or user interactions.

ngContainer

ng-container is a special Angular element that acts as a grouping element for structural directives but doesn't interfere with styles or layout because Angular doesn't put it in the DOM. This element is particularly useful for cases where you want to apply a structural directive (like ngIf, ngFor, ngSwitch) but don't want to add an extra element to the DOM, which might disrupt your layout or styling.

ngFor

ngFor is a built-in structural directive in Angular that is used to loop over a collection of items and create a template for each item in the list. It's commonly used to display dynamic data in the form of lists or tables in Angular applications.

ngIf

ngIf is another important structural directive in Angular. It is used to conditionally add or remove an element from the DOM based on a specified condition. This directive plays a crucial role in controlling what parts of your application are visible to the user at any given time.

ngStyle

ngStyle is a built-in directive in Angular that allows you to set inline styles dynamically on an HTML element based on a given condition or expression. This directive is particularly useful for changing the style of an element in response to component state changes, user interactions, or other dynamic conditions in your application.

ngSwitch

ngSwitch is a structural directive in Angular that provides a way to add or remove DOM elements based on an expression. It is similar to the switch statement in many programming languages and is used for displaying one of several possible views based on a given condition. This directive is particularly useful for situations where you have multiple conditions that might lead to different UI elements being displayed.

Structural directives

Angular's structural directives are a powerful feature that manipulate the DOM by adding, removing, or manipulating elements. These directives shape or reshape the DOM's structure, typically by adding, removing, or manipulating elements. The most commonly used structural directives in Angular are ngIf, ngFor, and ngSwitch. Let's dive into each of these: