Home > Mobile >  Angular merge does not fire all observables within switchMap
Angular merge does not fire all observables within switchMap

Time:06-01

I am merging two observables in a switchMap operator but my problem is that only one observable fires. My code

    formArray.valueChanges
        .pipe(
            takeUntil( this._unsubscribeAll ),
            switchMap( sm => {
                const tempAnswerChanges$: Observable<any>[] = formArray.controls.map( ( c, i ) => {
                    return c.get( 'answer' ).valueChanges
                        .pipe(
                            tap( response => {
                                this.value = !this.value
                                formArray.controls[ 2 ].patchValue( { display: this.value }, { emitEvent: true } )
                                console.log(formArray.controls[ 2 ].value)
                            } ),
                        )
                } );
                const tempDisplayChanges$: Observable<any>[] = formArray.controls.map( ( c, i ) => {
                    return c.get( 'display' ).valueChanges
                        .pipe(
                            tap( response => {
                                console.log('Display Has changed',response)
                            } ),
                        )
                } );
                return merge(...tempDisplayChanges$, tempAnswerChanges$ );
            } ),
        ).subscribe();

In the example if the change the merge statement to

  return merge(...tempAnswerChanges$, tempDisplayChanges$ );

then the tempAnswerChanges$ fires and the tempDisplayChanges$ never fires. I don't understand why

CodePudding user response:

Try this:

return merge(...tempAnswerChanges$, ...tempDisplayChanges$ );
  • Related