Piping inside a subscribe in ngrx

喜欢而已 提交于 2019-12-13 06:29:38

问题


I have a selector that takes a parameter to filter values.

The parameter depends on an observable's return.

export class LineSynopticComponent implements OnInit, AfterViewInit {

schedules$: Observable<ISchedule[]>;

ngOninit(){
  this.selectedDate$.subscribe(elm => {
  this.schedules$ = this.store.pipe(select(selectSchedulingsTimes, { time: elm.timeSelect }));
});
}

the selectSchedulingsTimes selector is defined as well:

const schedulings = (state: IAppState) => state.schedulings;

export const selectSchedulingsTimes = createSelector(
  schedulings,
  (state: ISchedulesState, { time }: { time: string }) => {
    let nowFormat = moment(time, 'HH:mm');
    return state.schedulings.data.filter(elm => {
      ... 
      return elm
    });
  }
);

in a second time I subscribe to schedules$

this.schedules$.subscribe((schedules: ISchedule[]) => {

  ...

}

I get inside selectSchedulingsTimes selector only once when application is started, but when I change values of selectedDate$ the selector is not triggered so I'm not entering in selectSchedulingsTimes.

How can I make selector send new data every change on selectedDate?

Is it because I'm not subscribing to schedules$ ?

Don't hesitate to ask me for unclear parts


回答1:


Let's change your observable setup like this:

ngOninit(){

    this.selectedData$.pipe(
      switchMap((elm) => {
        return this.store.pipe(select(selectSchedulingsTimes, { time: elm.timeSelect }));
      })
    ).subscribe((resposeFromStore) => {
      //Do whatever you want tot do with the store value
      console.log(resposeFromStore);      
    })    
  }

You need not subscribe on selectedData$ and then set up your other observable. Hope it helps.




回答2:


I think your selector is the problem. You should read this, its giving some good example and it also contains a section regarding dynamic parameters. Thats what I would try:

export const selectSchedulingsTimes = createSelector(
  schedulings,
  schedule => (time: string) => {
    let nowFormat = moment(time, 'HH:mm');
    return state.schedulings.data.filter(elm => {
      ... 
      return elm
    });
  }
);


来源:https://stackoverflow.com/questions/57267134/piping-inside-a-subscribe-in-ngrx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!