Passionate Coder
Passionate Coder

Reputation: 7294

How to pass id in effects in angular ngrx

I am selecting a list and based on that I need to display selected list info.

Upvotes: 2

Views: 383

Answers (1)

Prashant Pimpale
Prashant Pimpale

Reputation: 10697

I am really not familiar with the ngrx just tried to subscribe to the observable and get the last object:

View() method:

constructor(private store: Store<IUserState>) {}

public user$: Observable<IUser[]>; -- declare once 

view(id) {
  this.store.dispatch(fromUserActions.loadUser({ id: id }));
  this.store.select(userQuery.getEntity).subscribe(value => {
    this.user$ = value[value.length - 1]; -- and assing the data as per values
    console.log(value[value.length - 1]);
  });
}

HTML:

<pre *ngIf="userLoaded$">
    {{user$ | json}}
</pre>  -- Removed async pipe

EDIT:

view(id) {
    this.store.dispatch(fromUserActions.loadUser({ id: id }));
    this.store.select(userQuery.getEntity).subscribe(value => {
      this.user$ = new Observable<IUser>();
      this.user$ = value.filter(x=> x.id == id)[0];
      console.log(value.filter(x=> x.id == id)[0]);
    });
}

Demo

Upvotes: 1

Related Questions