Skip to content

Instantly share code, notes, and snippets.

@mraible
Created January 23, 2018 19:44
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 mraible/4c83687805a64fe94c517b23f5d371d0 to your computer and use it in GitHub Desktop.
Save mraible/4c83687805a64fe94c517b23f5d371d0 to your computer and use it in GitHub Desktop.
Unsubscribe in an Angular component
import { Component, OnInit } from '@angular/core';
import { CarService } from '../shared/car/car.service';
import { GiphyService } from '../shared/giphy/giphy.service';
import { Subscription } from 'rxjs/Subscription';
@Component({
selector: 'app-car-list',
templateUrl: './car-list.component.html',
styleUrls: ['./car-list.component.css']
})
export class CarListComponent implements OnInit {
cars: Array<any>;
sub: Subscription;
constructor(private carService: CarService, private giphyService: GiphyService) {
}
ngOnInit() {
this.sub = this.carService.getAll().subscribe(data => {
this.cars = data;
for (const car of this.cars) {
this.giphyService.get(car.name).subscribe(url => car.giphyUrl = url);
}
});
}
ngOnDestroy() {
this.sub.unsubscribe();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment