Common Directives
Angular’s@angular/common package provides essential directives for manipulating the DOM structure and styling elements in your templates.
Structural Directives
Structural directives change the DOM structure by adding or removing elements.NgIf
Conditionally includes a template based on the value of an expression.- Syntax
- Examples
- Migration
packages/common/src/directives/ng_if.ts:166
NgFor
Renders a template for each item in a collection.- Syntax
- Examples
- Context Variables
- Migration
packages/common/src/directives/ng_for_of.ts:177
NgSwitch
Switches between views based on a matching expression.- Syntax
- Examples
- Migration
packages/common/src/directives/ng_switch.ts:120
As of Angular v17,
NgSwitch uses strict equality (===) instead of loose equality (==) for matching cases.Attribute Directives
Attribute directives modify the appearance or behavior of DOM elements without changing the structure.NgClass
Adds and removes CSS classes on an HTML element.- Syntax
- Examples
- Component
packages/common/src/directives/ng_class.ts:79
NgStyle
Updates styles for the containing HTML element.- Syntax
- Examples
- Component
packages/common/src/directives/ng_style.ts:64
Usage Notes
Importing Directives
Performance Considerations
NgFor Performance
NgFor Performance
Always use
trackBy with *ngFor when rendering lists that may change:Change Detection
Change Detection
NgClassandNgStyleimplement custom change detection for deep object comparison- Use immutable data patterns for better performance
- Consider using direct bindings for simple cases
See Also
Control Flow
Modern control flow syntax
Template Syntax
Angular template guide
Structural Directives
Creating custom directives
Pipes
Common pipes reference
