behaviorsubject

Emit and broadcast events throughout application in Angular

我的梦境 提交于 2019-12-06 13:20:55
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? Günter Zöchbauer In Angular2 there is no global scope. There are only components and services. With services you can define their scope by the place

How to share data between components using service and observable?

十年热恋 提交于 2019-12-06 11:12:29
Hi I am new to angular 2+, I am trying to share data between two components but the second component is not retrieving the data from the service, it gets an empty object. Service - using rxjs BehaviorSubject to keep the object import { BehaviorSubject } from 'rxjs/BehaviorSubject'; import { Observable } from 'rxjs/Observable'; @Injectable() export class PostsService { response: any = {}; private messageResponse = new BehaviorSubject(this.response); currentResponse = this.messageResponse.asObservable(); constructor(private http: Http) { } // Get all posts from the API getAllPosts() { return

BehaviorSubject is executed more than once

心不动则不痛 提交于 2019-12-02 06:22:51
I am using angular 4 for my app and I use behaviorSubject to communicate between component, everythings were working fine Here is my code: export class Globals { loadStudentsByClass: BehaviorSubject<string> = new BehaviorSubject<string>(null); } export class ClassComponent implements OnInit { selectNewClass() { console.log('About to select new class', this.selectedClass); this.globals.loadStudentsByClass.next(this.selectedClass); this.globals.changeClassBehavior.next(true); this.router.navigateByUrl(''); } } export class StudentComponent implements OnInit { this.globals.loadStudentsByClass

Angular 4 - rxjs BehaviorSubject usage in Service

有些话、适合烂在心里 提交于 2019-12-01 23:49:19
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.getAccountInfo"); this.http .post(this.clientAccountInfoURL, body, options) .map((response : Response) =>

Simple way to get the current value of a BehaviorSubject with rxjs5

混江龙づ霸主 提交于 2019-11-30 21:26:47
问题 Previously in rxjs4 there was a method in the BehaviorSubject called: getValue() (doc here). This method does not exist any more in rxjs5 . So the only solution that I found to get the value of a BehaviorSubject was: let value; myBehaviorSubject.take(1).subscribe( (e) => value = e ); This code run synchronously (I do not exactly understand why, but it does ...) and get the value. It work, but it's not as clean as it could be if getValue() was present: let value = myBehaviorSubject.getValue();

How to create behavior subject for object and subscribe to it on another component?

扶醉桌前 提交于 2019-11-29 22:35:32
问题 I have created a behaviour subject in a service class. public personObject: BehaviorSubject<any> = new BehaviorSubject<any>({ personId: 1, name: 'john doe' }); On a component that imports this service, i have subscribed this behavior subject like this: ` this._subscription.add( this._bankService.personObject.subscribe(data => { this.personObject = data; console.log(data); }) );` But I am not able to get exact data set in behavior subject. Please help. Edit I forgot to mention that i have used

Using a single subscription variable with BehaviorSubject

心已入冬 提交于 2019-11-29 13:05:42
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 DetailComponent implements OnInit { subscription; //??? Can I use this variable for every subscription

BehaviorSubject vs Observable?

与世无争的帅哥 提交于 2019-11-25 23:02:48
问题 I\'m looking into Angular RxJs patterns and I don\'t understand the difference between a BehaviorSubject and an Observable . From my understanding, a BehaviorSubject is a value that can change over time (can be subscribed to and subscribers can receive updated results). This seems to be the exact same purpose of an Observable . When would you use an Observable vs a BehaviorSubject ? Are there benefits to using a BehaviorSubject over an Observable or vice versa? 回答1: BehaviorSubject is a type