#How to pass parameters to fromEvent (rxjs) from a button click?

1 messages · Page 1 of 1 (latest)

maiden plover
#

I have a ngfor looping on a dynamic collection of object to display some data.
I attach an event handler (fromEvent(button.nativeElement, 'click')) on it.
I need to retrieve the id of the object on the event to use it in the observable but i don't know how to do it.
I could use the id of the button since it's accessible from the nativeElement but it seems a quite horrible thing to do.
Any idea?

vague ivy
#

This really doesn't seem like the angular way to do things. What's wrong with (click) on the buttons, in the template?

maiden plover
#

I need to wrap the httpCall of the button in an exhaustMap, i didn't succeed with the click(). I succeed this way with no parameters though. But i'm open to other solutions 😄

#

With no parameters, i did this:
return Observable.fromEvent(this.theBtn.nativeElement, 'click').pipe(
exhaustMap(() => ......

#

I didn't succeed with click()

#

Edit: Ok actually it seems i need to use indeed click() but with subject, is that right?

vague ivy
#

Yes, that's the usual way of doing.

maiden plover
#

I lost 4 hours of my life looking for this. 😢

maiden plover
#

So in the end I have this (the private function is subscribed in the ctor)
onExpeditionSubject: Subject<void> = new Subject(); onExpeditionBtnClick() { this.onExpeditionSubject.next(); } private OnExpedition() { return this.onExpeditionSubject.pipe( exhaustMap(() => { return this.endpointsService.putTeamExpedition( this.team.teamId, this.team.difficultyLevel ); }), switchMap(() => { return forkJoin([ this.displayTeam(this.team.teamId), this.refreshExpeditionCost(), ]); }) ); }
Is it ok or there is a better way to achieve what i want?

cunning wyvern
#

Hello, I have some questions about your code.
1.) there is one button for each item of your loop ?
2.) you said the private function is subscribe in the constructor. What is the this.team attribute ? Is an Input of your component ?
3.) what is the goal of your refreshExpeditionCost method ?