Swiftorial Logo
Home
Swift Lessons
AI Tools
Learn More
Career
Resources

Angular FAQ: Top Questions

45. How to share data between components in Angular?

  • Parent to Child: Use @Input() decorator.
  • Child to Parent: Use @Output() and EventEmitter.
  • 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);