0

I am working with Angular 5, in my i am doing as below

ngOnInit() {
        this.activatedRoute.parent.params.subscribe(
            params => this.branchId = +params['id']
        );
        this.validateAssignForm();
        this.getConfirmReturnValue();
    }

getConfirmReturnValue() {
      this.subscription1 = this.confirmService.getReturnValue()
        .subscribe(
            suc => {
                console.log('return value::', suc);
                if (suc != 0 && suc.hasOwnProperty('returnValue')) {
            this.deleteLanguage(suc); }
        });
    }

but i am not unsubscribing activatedRoute any where in my component as it will unsubscribe by Angular.

In my code i am subscribing BehaviorSubject through shared service. now i am unsubscribing BehaviorSubject in ngOnDestroy() {} function as below

ngOnDestroy() {
        this.subscription1.unsubscribe();
    }

my question is can i unsubscribe BehaviorSubject like activateRoute (without using ngOnDestroy()) while destroying the current component.

Santhosh
  • 1,053
  • 1
  • 20
  • 45
  • 2
    https://stackoverflow.com/questions/38008334/angular-rxjs-when-should-i-unsubscribe-from-subscription – martin Apr 03 '18 at 11:06
  • @marin, i knew about this solution but, my BehaviorSubject should unsubscribe itself while destroying component from DOM. with out writing ngOnDestroy(){} it should unsubscribe like activateRoute – Santhosh Apr 03 '18 at 11:19
  • I don't think there's any way to do it without using `ngOnDestroy` – martin Apr 03 '18 at 11:37
  • thanks @martin, will try and let you know – Santhosh Apr 03 '18 at 11:56
  • I think you should unsubscribe in both cases. A partial solution would be to add `take(1)` but you can probably guess what side-effects that has. – Robin Dijkhof Apr 04 '18 at 06:48

0 Answers0