angular subscribe auto unsubscribe code example

Example 1: angular unsubscribe from observable

//Please note that there are many ways to unsubscribe, this is one of them
import { Subscription } from 'rxjs';

private searchEventSubscription: Subscription;

export class someComponent OnInit, OnDestroy {

    constructor(private someService: SomeService) { }

    ngOnInit() {
      this.searchEventSubscription = this.someService.someMethod.subscribe(result => {  
        doSomething(result);
      });
    }

    ngOnDestroy() {
		this.searchEventSubscription.unsubscribe()
    }

Example 2: clean up an angular subscription

@Component({...})export class AppComponent implements OnInit {    subscription: Subscription    ngOnInit () {        var observable = Rx.Observable.interval(1000);        this.subscription = observable.subscribe(x => console.log(x));    }}