Angular FAQ: Top Questions
45. How to share data between components in Angular?
- Parent to Child: Use
@Input()
decorator. - Child to Parent: Use
@Output()
andEventEmitter
. - Between Unrelated Components: Use a shared service with observables.
// Parent to Child
<child [data]="parentData"></child>
// Child to Parent
@Output() notify = new EventEmitter<string>();
this.notify.emit('data');
// Service
this.dataService.data$.subscribe(data => this.value = data);