@angular/cdk/observers package provides utilities for observing DOM changes, including content changes and element mutations.
Installation
ContentObserver
Observes text content changes within an element.Using the Service
Using the Directive
MutationObserverFactory
CreatesMutationObserver instances with proper cleanup.
API Reference
ContentObserver
| Method | Returns | Description |
|---|---|---|
observe(element) | Observable<MutationRecord[]> | Observe content changes |
cdkObserveContent Directive
Selector:[cdkObserveContent]
| Input | Type | Description |
|---|---|---|
cdkObserveContentDisabled | boolean | Disable observation |
debounce | number | Debounce time in ms |
| Output | Type | Description |
|---|---|---|
cdkObserveContent | MutationRecord[] | Emits on content change |
Use Cases
Auto-resize Textareas
Dynamic Tooltip Positioning
Best Practices
- Unsubscribe on destroy - Prevent memory leaks
- Use debounce - Avoid excessive updates
- Observe specific changes - Configure MutationObserver options
- Consider performance - Observation has overhead