40Hz_ringtone 40Hz_ringtone - 11 months ago 96
AngularJS Question

Angular 2 Array Subject .next() not invoking another component's observer

I'm using firebase as my backend.

Inside of a data.service.ts, I create a Subject array which will be filled by the firebase observer on app init:

private orders = new Subject<any>();
orders$ = this.orders.asObservable();

firebase.database().ref(this.fbDataPath).on('child_added', (childSnapshot) => {
key: childSnapshot.key,
name: childSnapshot.val().name,
items: childSnapshot.val().items

I then provide a separate directory component with DataService and subscribe to its orders observable:

order => {
console.log('subscribe hit')

I can't seem to get the listener component to trigger on a next. I made this work for a boolean isLoggedIn, and I must be missing something in this scenario. Thanks!

Answer Source

It might be because you're using this in a closure. Remove the this from this.orders.next()

EDIT: Try this

var self = this;
firebase.database().ref(this.fbDataPath).on('child_added', (childSnapshot) => {