Skip to content

Instantly share code, notes, and snippets.

@rahulsahay19
Last active January 25, 2018 17:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rahulsahay19/9c0b128290d14efc20b98ff22ced3ed2 to your computer and use it in GitHub Desktop.
Save rahulsahay19/9c0b128290d14efc20b98ff22ced3ed2 to your computer and use it in GitHub Desktop.
observable
import { Component,OnInit } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { OnDestroy } from '@angular/core/src/metadata/lifecycle_hooks';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit, OnDestroy {
title = 'app';
observable$;
ngOnInit(){
this.observable$ = Observable.create((observer) => {
observer.next(1);
observer.next(2);
observer.next(3);
observer.complete();
});
this.observable$.subscribe(
value => console.log(value),
err => {},
() => console.log('this is the end')
);
}
//This is to make sure observable should not live when move to different pages.
//This makes sure no memory leaks
ngOnDestroy(): void {
this.observable$.unsubscribe();
}
//A subject is both observable and observer.once you define a subject, it means you will have access
//to next, complete outside observable.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment