How to pass data between components in angular

sharing data between components angular 8
sharing data between components angular 7
sharing data between components angular 6 using service
how to pass data from one component to another in angular 6
how to pass data from one component to another in angular 7
angular 7 communication between sibling components
sharing data between components angular 8 using service
sharing data between components angular 7 using service
<div class="container-fluid fill">
<div class="container fill">
  <div class="row padding">
    <div class="col-md-3 ">
    <div class="col-md-6 text-center">
    <div class="col-md-3 ">

app-sidebar renders a category menu and when a category is Selected I want to pass the value to menu section component to render appropriate category

So how to pass the value between two components which are not parent child

Use an @Output Event emitter to emit the selected category from the app-sidebar component.

Then you can write an event hanlder for that in the host component.

In app-sidebar component.ts

@Output() categorySelect = new EventEmitter<any>(); //put the data type of category here.

//use this method to set the selected category
    public onSelectionChange(data): void {

On host.component.html file.

<div class="col-md-3 ">
  <app-sidebar (categorySelect)="onCategorySelect($event)"></app-sidebar>
  <app-menu-section [selectedCategory]="selectedCategory"></app-menu-section>

on host-component.ts file

public onCategorySelect(data): void {
 this.selectedCategory = data;

In app-menu-section.ts file

@Input()  selectedCategory: any;

Hope this helps. For more info read this;

Sharing Data Between Angular Components, Alright, what if you want to pass data from the child component to the parent component? Child to parent via @Output() and  Passing data between components in angular is obvious while working on the angular project. The flow of the data among the components can be on different directions based on the requirement of the project. Child component to Parent component or Parent Component to Child component.

there are many way you can do this. I suggest you to define a get function inside the controller of your app-sidebar component, like 'getSelectedItem' that return the value of the selected item. So now you can recover the selected value at any time thanks to the binding, and you can pass the value to the other component.

Therefore, in your app-sidebar component you can define the binding

binding: {
   getSelectedValue: '='

In the controller you can now define the get-function

thsi.getSelectedValue = {
   // do your stuff here
   return selectedValue

Now the crucial passage: In the main controller (I mean the controller of the html page in which you use both component) you shoul declare a function, just to tell angular that this function exist, but it is not define here (infact we want to call the function that is declared in the component to recover the selected value). So in your controller you have just to write


Now in the html page you can use your modified component in this way:

<app-sidebar get-selected-value='mainController.getValue'></app-sidebar>

(Remember the kebab notation: getSelectedItem to get-selectd-item)

This is kind of a revers binding. Infact you normally define a function in your main controller and then you pass it to your component. In this case the function is define in your component, but thanks to the binding now you can call the function getSelectedItem in your main controller calling this.getValue(); Infact the function this.getValue() is not defined in the controller, it is just declared, but it is in binding with the function getSelectedValue.

At this point you have your value and you can pass it to the other component.

Maybe this method is a bit longer than other, but I suggest to use this because in many occasion it is very useful to use this 'reverse' binding.

Sharing data between components in Angular. - Onejohi, Component interactionlink This cookbook contains recipes for common component communication Pass data from parent to child with input binding​link The History log demonstrates that messages travel in both directions between the  Inside the .component in the model that you just passed data into (ModelOne), use the same name as you declared in the HTML (‘data’) and add it into a bindings object (note: bindings, plural). You can use ‘<’, ‘=’, ‘&’ or ‘@’ depending on what you need.

Atlernative you can share parameter as observable in your service. Just inject service into your components and subscribe. On category select update observable and all subscribers will be informed.

export class CategoryService {
  emitter: EventEmitter;
  category$: Observable<Category>;

  constructor() { 
    this.$category = Observable.create(e => this.emitter = e);

  getCategory$(): Observalbe<Category> {
    return this.$category;

  selectCategory(category: Category): void {

Sharing Data between Angular Components, Parent to Child: Sharing Data via Input This is probably the most common and straightforward method of sharing data. It works by using the  Passing data between components is a two way process. In your case, let's say home.ts contains an object named as data. 1.In the home.component.html , where you used <map-component></map-component>, replace it with <map-component [data]="data"></map-component>.

You can do it by passing data up and down your component chain as described by Anuradha Gunasekara as long as you keep in mind that your app may become cumbersome and overly complex. Of course, it all depends on the context and what you want to create. I've made this mistake as a beginner with Angular in the past and found myself with a tangled mess to trace once things have grown.

Component interaction, Angular — Sharing Data between components. Angular is popular framework for developing Single page web application. I am here explain  Passing data between Angular 2 Components 1. Sending data from ParentView into childView : inputs. Now we have included parentView component into our main.ts file and we have used <app-parent-view> the selector to render the parentView template.

How to pass data between two components in Angular 2, How do you do pass that function to the child component? For those who are unfamiliar with Angular, passing data between the components is  In this article, you will learn about passing the data between components, using @Output,@Input and EventEmitter in Angular. In Angular, we can use any component inside another component. Here, the component, which is used inside other component is called child component and the main component is called parent component.

Passing data between components in angular, Unrelated Components: Sharing Data with a Service. When passing data between components that lack a direct connection, such as siblings, grandchildren, etc, you should you a shared service. When you have data that should aways been in sync, I find the RxJS BehaviorSubject very useful in this situation.

Sharing Data between Components in Angular, 3 Answers 3. As it looks like you are looking to redirect to those components, what you can do is have an event emitter on component one, that on click will emit the data to the parent(of all 3). Then in the parent you would catch the emit, and assign that to data that you pass into the other components.