Skip to content

Instantly share code, notes, and snippets.

@shotaK
Last active December 14, 2017 12:04
Show Gist options
  • Save shotaK/2cb58a6f249ccf46bdcd9722621caefc to your computer and use it in GitHub Desktop.
Save shotaK/2cb58a6f249ccf46bdcd9722621caefc to your computer and use it in GitHub Desktop.
@Component({
template: `
<my-widget>
<comp-a/>
</my-widget>
`
})
class App {}
@Component({
selector: 'my-widget',
template: `<comp-b/>`
})
class MyWidget {}
/*
From my-widget's perspective, comp-a is the ContentChild and comp-b is the ViewChild.
CompomentChildren and ViewChildren return an iterable while the xChild return a single instance.
*/
// Forms - This also applies to any form control.
export class TestComponent {
ngOnInit() {
this.form = new FormGroup({...});
this.valueChanges = this.form.valueChanges.subscribe(console.log);
this.statusChanges = this.form.statusChanges.subscribe(console.log);
}
ngOnDestroy() {
this.valueChanges.unsubscribe();
this.statusChanges.unsubscribe();
}
}
// The Router
// ActivatedRoute does not need to be unsubscribed
// https://github.com/angular/angular/issues/13804
export class TestComponent {
constructor(private route: ActivatedRoute, private router: Router) { }
ngOnInit() {
this.route.params.subscribe(console.log);
this.route.queryParams.subscribe(console.log);
this.route.fragment.subscribe(console.log);
this.route.data.subscribe(console.log);
this.route.url.subscribe(console.log);
this.router.events.subscribe(console.log);
}
ngOnDestroy() {
// You should unsubscribe from each observable here
}
}
// Renderer Service
export class TestComponent {
constructor(private renderer: Renderer2,
private element : ElementRef) { }
ngOnInit() {
this.click = this.renderer.listen(this.element.nativeElement, "click", handler);
}
ngOnDestroy() {
this.click.unsubscribe();
}
}
// Infinite Observables
// When you have an infinite sequence, you should unsubscribe (unless you have a special case),
// for example when using the interval() or the fromEvent() observables.
export class TestComponent {
constructor(private element : ElementRef) { }
interval: Subscription;
click: Subscription;
ngOnInit() {
this.interval = Observable.interval(1000).subscribe(console.log);
this.click = Observable.fromEvent(this.element.nativeElement, 'click').subscribe(console.log);
}
ngOnDestroy() {
this.interval.unsubscribe();
this.click.unsubscribe();
}
}
// DO NOT UNSUBSCRIBE
// Async pipe
@Component({
selector: 'test',
template: `<todos [todos]="todos$ | async"></todos>`
})
export class TestComponent {
constructor(private store: Store) { }
ngOnInit() {
this.todos$ = this.store.select('todos');
}
}
// @HostListener
export class TestDirective {
@HostListener('click')
onClick() {
}
}
// Finite Observable
// When you have a finite sequence, usually you don’t need to unsubscribe,
// for example when using the HTTP service or the timer observable.
export class TestComponent {
constructor(private http: Http) { }
ngOnInit() {
Observable.timer(1000).subscribe(console.log);
this.http.get('http://api.com').subscribe(console.log);
}
}
// How to unsubscribe HTTP service component
import { Component, OnDestroy, OnInit } from '@angular/core';
import 'rxjs/add/operator/takeUntil';
// import { takeUntil } from 'rxjs/operators'; // for rxjs ^5.5.0 lettable operators
import { Subject } from 'rxjs/Subject';
import { MyThingService } from '../my-thing.service';
@Component({
selector: 'my-thing',
templateUrl: './my-thing.component.html'
})
export class MyThingComponent implements OnDestroy, OnInit {
private ngUnsubscribe: Subject = new Subject();
constructor(
private myThingService: MyThingService,
) { }
ngOnInit() {
this.myThingService.getThings()
.takeUntil(this.ngUnsubscribe)
.subscribe(things => console.log(things));
/* if using lettable operators in rxjs ^5.5.0
this.myThingService.getThings()
.pipe(takeUntil(this.ngUnsubscribe))
.subscribe(things => console.log(things));
*/
this.myThingService.getOtherThings()
.takeUntil(this.ngUnsubscribe)
.subscribe(things => console.log(things));
}
ngOnDestroy() {
this.ngUnsubscribe.next();
this.ngUnsubscribe.complete();
}
}
// If you are using observable streams via the async pipe
// then you do not need to worry about unsubscribing.
// The async pipe will take care of subscribing and unsubscribing for you.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment