'ngOnInit having multiple NgRx store subscription

Am working on angular 13 version.I have 3 component. Two of them having downtown list.While selecting these drop down, i will save selected value to NgRx store. In my third component ,i have to subscribe selected dop-down value from the store. Based on the above store value i need to pass these value as a param to my backed API

This is my ngOnInit code

ngOnInit(): void {
    this.co1figFacade.getSeleConSet().subscribe((data: string) => {
      if (data.length > 0) {
        this.cuConfSet = data;
      }

    });
     
    this.coFacade.getCurrentCoType().subscribe((data: string) => {
      if (data.length > 0) {
        this.conType = data;
      }
    
    });

    if(this.conType .length>0 && this.cuConfSet.length>0){
      
      this.getData(this.cuConfSet,this.conType );
    }
  }

How can i solve this issue

Am trying to do below steps

1.subscribing two method (getSeleConSet and getCurrentCoType)These two method return a string value.

2 if the two response are not null.i need to pass these two response as an argument to getData method



Solution 1:[1]

you need to use combineLatest to connect both observables and then use tap to check and do something with the data returned.

ombineLatest(
  this.co1figFacade.getSeleConSet(),  
  this.coFacade.getCurrentCoType()
 )
.tap(([v1, v2]) => {
  if(v1 && v2) {
    this.getData(this.cuConfSet,this.conType );
  }
}).subscribe();

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Eli Porush