user9298624
user9298624

Reputation:

Change Detection and Events from outside of Angular

I have created and Angular 7 Application that uses a very large external library. I have all thing wired up with success with the exception of an event from that external sources.

I have an event listener on that external sources that fires and sends data to my Angular App.

externalApp.event((data)=>{
  this.externalMonitorService.newData(external.data)
});

That works fine.

This function then uses a BehaviorSubject and emits the data

newData(data){
  this.mySubject.next(data);
}

That is working fine as well.

I can subscribe to this in any component and I get changes, it works fine.

However, in my template I can not use async pipe as it does not detect the changes. I have to ChangeDetectorRef and update a variable and then call detectChanges.

I don't want to have to worry about this subscription, to have to implement this logic in every component, it is very ummmm... reactive breaking.

Am I missing something deep in Angular I do not know about? Is there a way in angular, perhaps NgZone where I can call something that lets Angular know, this data is coming from an external source treat this as a change, thus I will not have to implement this logic everywhere.

Upvotes: 3

Views: 1485

Answers (1)

Artem Arkhipov
Artem Arkhipov

Reputation: 7455

Probably you should inject ngZone and wrap the call into its run method.

import { NgZone } from '@angular/core';

constructor(
   private ngZone: NgZone
  ) {}

this.ngZone.run(() => this.externalMonitorService.newData(external.data));

Upvotes: 4

Related Questions