Created
January 23, 2018 19:44
-
-
Save mraible/4c83687805a64fe94c517b23f5d371d0 to your computer and use it in GitHub Desktop.
Unsubscribe in an Angular component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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