1

I have a service to share the variables among the components as follows,

import { Injectable } from '@angular/core';
import { Subject } from 'rxjs/Subject';

@Injectable()
export class AppMessageQueuService {    
    checkOutPageParams: Subject<Object>;
    constructor() {     
        this.checkOutPageParams = new Subject<string>();

    }

}

I use this service to pass the following parameters to another component,

  this.appMsgService.checkOutPageParams.next({
            'userId': listItem.userInfo.userId,
            'listingId': listItem.listingId,
            'event': this.event
        });

I can see the values being assigned to event when i debug, this is how i get the values in the 2nd component,

 this.appMsgService.checkOutPageParams.asObservable()
            .switchMap((params: Object) => {
                let userId = params['userId'];
                let listingId = params['listingId'];
                this.event = params['event'];
                return this.checkOutInforService.getCheckoutDetails(userId, listingId);
            }).subscribe((checkoutDetail: CheckoutUserDetail) => {
                this.checkoutInfoDetails = checkoutDetail;
            });
            console.log(this.event) //this returns undefined

I can see the value being assigned to event as well. but when i print a console.log(this.event) after the above lines, it says undefiend.

What is the issue. how can i assign the value to the event and use it within the component 2.

Sajeetharan
  • 216,225
  • 63
  • 350
  • 396

1 Answers1

2

I'm not sure if this is the best way, but you could try:

import 'rxjs/add/observable/forkJoin'


this.appMsgService.checkOutPageParams.asObservable()
            .switchMap((params: Object) => {
                return Observable.forkJoin ([
                  this.checkOutInforService.getCheckoutDetails(userId, listingId),
                  Observable.of(params)])
            }).subscribe((result) => {
                this.checkoutInfoDetails = result[0] as CheckoutUserDetail;
                this.params = result[1];
                ... do with params as you wish ...
            });
Michael Kang
  • 52,003
  • 16
  • 103
  • 135