Exchange data between components using service

angular 6 years, 3 months ago
//service.ts import { Injectable } from '@angular/core'; import { BehaviorSubject } from 'rxjs' @Injectable({ providedIn: 'root' }) export class multiSelectService { constructor() { } private filterType=new BehaviorSubject<boolean>(null); filterObservable= this.filterType.asObservable(); setfilterType(type:boolean){ this.filterType.next(type); }} //To get values from observable, first initialise service variable. private serviceVar:multiSelectService; //Then, subscribe to filterObservable to get value in filtertype. this.multiSelectDialogService.filterObservable.subscribe(data=>{ this.filterType=data; this.dialogFilterType=data; this.diplayData() }); //To set value to filterType this.serviceVar.setfilterType(true);
1124
Posted By