Templates & Directives
Angular templates define the HTML structure of a component. Directives are special instructions that modify the DOM behavior or appearance in your templates. Directives are of two main types: Structural Directives (change DOM structure) and Attribute Directives (change element appearance or behavior).
1. Structural Directives
Structural directives change the layout or structure of the DOM.
*1.1 ngIf
Used to conditionally show or hide elements.
*1.2 ngFor
Used to render lists dynamically.
*1.3 ngSwitch
Used to conditionally display one element from multiple options.
2. Attribute Directives
Attribute directives change the appearance or behavior of elements without changing the DOM structure.
2.1 ngClass
Adds or removes CSS classes dynamically.
2.2 ngStyle
Dynamically sets inline CSS styles.
2.3 Custom Directives
You can create your own directives to add custom behavior.
Example: HighlightDirective
Usage in template:
Summary
- Templates define the UI structure of a component.
- Structural Directives (
*ngIf,*ngFor,*ngSwitch) manipulate DOM elements. - Attribute Directives (
ngClass,ngStyle) change the appearance of elements. - Custom directives allow you to implement reusable behavior across your application.