behaviorsubject

Angular 4 - rxjs BehaviorSubject usage in Service

南楼画角 提交于 2019-12-20 03:31:11
问题 My Service code looks like below - DataService @Injectable() export class DataService { ... private serviceRequestDtoSource = new BehaviorSubject<ServiceRequestDto>(null); serviceRequestDto$ = this.serviceRequestDtoSource.asObservable(); ... getAccountInfo(serviceRequestDto : ServiceRequestDto){ let body = JSON.stringify(serviceRequestDto); let headers = new Headers({ 'Content-Type': 'application/json' }); let options = new RequestOptions({ headers: headers }); console.log("In data service

Using a single subscription variable with BehaviorSubject

流过昼夜 提交于 2019-12-17 21:29:23
问题 I use BehaviorSubject in my Angular app and I get observable to my Details component from DataService as shown below: DataService.ts: export class DataService { private messageTracker = new BehaviorSubject<any>(); private fileTracker = new BehaviorSubject<any>(); getMessageTracker(): Observable<any> { return this.messageTracker.asObservable(); } getFileTracker(): Observable<any> { return this.fileTracker.asObservable(); } //set methods omitted for brevity } DetailComponent : export class

How to build a service that gets values async but also perfoms tasks on that data, like filtering?

白昼怎懂夜的黑 提交于 2019-12-13 03:54:19
问题 I have a service " provided in root " that: gets data once from an API should handle a mapping on that data should provide that data to other components if needed This data doesn't need to be reloaded throughout the lifecycle of the app. It's only reloaded when the user refreshes the browser. All of the data is going to be used at some point - so it makes no sense to make http requests on each getOneById() . A filter is faster in this case. The mockup of the service looks something like this:

How to use Subjects to share global data in angular 6

こ雲淡風輕ζ 提交于 2019-12-12 13:14:23
问题 I have been trying to find the best solution to share some data beetween two componentes that share the same parent. I am using an angular-material stepper. Step 1 has one component and step 2 has another one. What i am trying to do is to "click" 1 button in the component from the step 1 and refresh an array of data that affects component in step 2. This is my code: Global service: export class GlobalDataService { private reserveSource = new BehaviorSubject<any[]>([]); currentReserve = this

Emit and broadcast events throughout application in Angular

大兔子大兔子 提交于 2019-12-12 09:58:43
问题 Is there a way to emit event in angular2 that can be listened to in entire application? Like we had in AngularJS using $rootScope.broadcast and emit . Can this same thing be achieved in angular2? I read about @Output() and EventEmitter() and implemented it but it restricts only the parent to listen to the event emmitted by child. I read about BehaviorSubject being one of the ways to do this. Is that the right approach? Any other solution for this? 回答1: In Angular2 there is no global scope.

Communication between multiple components with a service by using Observable and Subject in Angular 2

你。 提交于 2019-12-12 04:45:38
问题 I am new to rxjs, so I would like to ask a question regarding Angular 2 , Observables and BehaviorSubject/Subject . So, what I want to achieve is : onclick of a button inside a ComponentA to notify other components for example ComponentB , ComponentC . What I did so far is to create a service : private isMenuOpenBS = new BehaviorSubject(false); toggleMenu(): void { this.isMenuOpenBS.next(!this.isMenuOpenBS.getValue()); } getMenuState(): Observable<boolean> { return this.isMenuOpenBS

ViewDestroyedError: Attempt to use a destroyed view

折月煮酒 提交于 2019-12-11 23:09:57
问题 (angular 8 in use) I have an issue currently with my code using a behaviour subject. Component A creates another component B trough the component factory. Now, Component A subscribes to component B it's status to know when to delete it. In this case, whenever there is an error in component B, Component A will change its own status and call Component B.destroy(). However, whenever I will run this destory method, I get an error in the logs saying ERROR Error: ViewDestroyedError: Attempt to use

Compare most recent values from multiple BehaviorSubjects

﹥>﹥吖頭↗ 提交于 2019-12-11 17:55:43
问题 Say I have this: isMatchedCountLessThanTotalCountMessage(){ // I want to implement this // "returns" a string asynchronously } getMatchedEventsCount() { return this.dcs.matchCount.asObservable(); } getTotalEventsCount() { return this.dcs.totalCount.asObservable(); } matchedCount and totalCount are like so: public matchCount = new BehaviorSubject<number>(0); public totalCount = new BehaviorSubject<number>(0); these Observables fire integers as values change. Anytime a value is fired from

Angular2: subscribe to BehaviorSubject not working

孤街浪徒 提交于 2019-12-11 05:08:41
问题 I have an Alert.Service.ts which saves alerts fetched from another service in an array. In another header.component.ts , I want to get the real-time size of that array. So, in Alert.Service.ts I have @Injectable() export class AlertService { public static alerts: any = []; // Observable alertItem source private alertItemSource = new BehaviorSubject<number>(0); // Observable alertItem stream public alertItem$ = this.alertItemSource.asObservable(); constructor(private monitorService:

Observables in nestjs - Reading a file asynchronously

孤街浪徒 提交于 2019-12-11 04:40:51
问题 I am trying a use case of reading a json file asynchronously and sending it out as a response (as a rxjs observable data). Here is the service that I use import { logger } from './../../shared/utils/logger'; import { Injectable } from '@nestjs/common'; import * as fs from 'fs'; import * as path from 'path'; import { BehaviorSubject, Observable, pipe, of, from, throwError, merge} from 'rxjs'; import { map, filter, scan, take, debounce, switchMap, retry, catchError, mergeMap, delay, zip, tap,