0

I'm learning how Observables work through Angular and Google Firestore.

this.allinvoicesCollection = this.afs.collection('clients').doc(this.clientId).collection('inbox').valueChanges();
    this.allinvoicesCollection.subscribe(val => {
      this.fileUrl = val[this.currentIndex].downloadURL;
    })

I'm fetching the fileUrl with the above code.

I want to update the fileUrl whenever the variable "this.currentIndex" changes however the above code obviously doesn't achieve that.

I understand that this only triggers when a doc is changed in the collection. How would I make it trigger when detecting change in "this.currentIndex"? I should mention that "this.currentIndex" is not an Observable just a simple number variable changed by the user.

Gsuz
  • 353
  • 3
  • 11

1 Answers1

0

make current index a Subject and the you can easily detect changes.

currentIndex= new Subject<number>();

and then when ever it changes you can push new data. suppose it changes either on button clik or if it is an input control then may be on keyup can push updated index.

onIndexChange(id:number){
    this.currentIndex.next(id);
}

For the second part while you are subscribing allinvoicesCollection use flatMap. You can check this answer for how to do nested subscriptions.

ashish pal
  • 467
  • 4
  • 10