How To Share Data Between Angular Components Using Rxjs Behaviorsubject.

    Table of Contents:
  1. 1. Child Component:
  2. 2. Demo:

To share Data Between Angular Components using RxJs BehaviorSubject, we would need to create a shared service. That way the Angular Application components can subscribe to this shared service and get the give data.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
// shared.service.ts
import { Injectable } from '@angular/core'; 
import { BehaviorSubject } from 'rxjs'; 
@Injectable() 
export class SharedService { 

data = { name: 'Otcollect', type: 'Website' } 

private dataSource = new BehaviorSubject(this.data); 
currentData = this.dataSource.asObservable();

 constructor(){} 

 changeData(newData: any){
 this.dataSource.next(newData); 
 }

}

The RxJs BehaviorSubject makes sure all components always receive the up to date data on subscription to dataSource. BehaviorSubject has a getValue() to get the values of data source. Now that we have our data source set with initial data, lets see how we can get this data in our parent components and children components. And also how we can change the data from both the parent components and children components. The <strong>dataSource BehaviorSubject</strong>, holds the current value of data. The currentData variable, handles our data as an observable that can be subscribe to by the components to get the current data. The <strong>changeData()</strong> method calls next on the <strong>dataSource BehaviorSubject</strong> to change the value.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
// parent.component.ts
import { Component, OnInit } from '@angular/core'; 
import { SharedService } from './shared.service'; 
@Component({
 selector: 'parentcomponent', 
 template: `{{data | json}}`
})
export class ParentComponent implements OnInit { 
data: any; 
constructor( private _sharedSer: SharedService ){} 
ngOnInit(){
 this._sharedSer.currentData.subscribe(data => this.data = data); 
} 
onChangeData(){ 
const newData = { name: 'Parent Component', type: 'Parent' } 
this._sharedSer.changeData(newData); } 
} 

Child Component:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
// child.component.ts
import { Component, OnInit } from '@angular/core'; 
import { SharedService } from './shared.service'; 
@Component({ 
selector: 'childcomponent', 
template: `{{data | json}}`
})
export class ChildComponent implements OnInit { 
data: any; 
constructor( private _sharedSer: SharedService ){} 
ngOnInit(){ 
this._sharedSer.currentData.subscribe(data => this.data = data); 
} 
onChangeData(){ 
const newData = { name: 'Child Component', type: 'Child' } 
this._sharedSer.changeData(newData); 
} 
}

In both the parent and child components, to get the current data, we simply inject the shared service, then subscribe to the <strong>currentData</strong> observable to get the value. Also, in both components, to change the current value of our data, we simply just call the changeData(newData: any) function, with new data. That's it!

Demo:

https://stackblitz.com/edit/angular-ztqjyz

Related Posts

0 Comments

12345

    00