-
-
Save av1v3k/556a97e9b553b29872458a1c8cb9a56f to your computer and use it in GitHub Desktop.
angular | scenario - clicking any button must cancel any existing API request.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //after click of any buttons in the web application, I need to cancel any existing API request and must trigger the new API request associated with the Button. How do I do it in angular ? | |
| import { Subject, switchMap } from 'rxjs'; | |
| // Inside your component | |
| private clickTrigger = new Subject<void>(); | |
| // Set up the stream in ngOnInit or the constructor | |
| this.clickTrigger.pipe( | |
| switchMap(() => this.myService.getData()) | |
| ).subscribe(data => { | |
| this.result = data; | |
| }); | |
| <button (click)="onButtonClick()">Fetch Data</button> | |
| onButtonClick() { | |
| this.clickTrigger.next(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment