Hello @kartik,
There're are basically two ways:
- call unsubscribe() on the Subscription object returned from the subscribe() call .
- use an operator
To just unsubscribe you could do it like this.
ngOnInit() {
this.subscription = timer(100, 100).subscribe(t => {
this.setFormData();
});
}
private setFormData() {
...
this.subscription.unsubscribe();
}
Or you can use Subject to complete the Observable via takeUntil() operator:
this.subject = new Subject();
ngOnInit() {
timer(100, 100).pipe(
takeUntil(this.subject),
).subscribe(t => this.setFormData());
}
private setFormData() {
...
this.subject.next();
}
Hope it helps!!
To know more about Angular, It's recommended to join Angular Training Certification today.
Thank you!!