Advertisement
andreadc

user-gallery.component.ts

Feb 28th, 2020
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Component, OnInit, Input } from '@angular/core';
  2. import { CarsService } from '../services/cars.service';
  3. import { pipe } from 'rxjs';
  4. import { filter, tap } from 'rxjs/operators';
  5. import { Car } from '../models/car';
  6.  
  7. @Component({
  8.   selector: 'app-user-gallery',
  9.   templateUrl: './user-gallery.component.html',
  10.   styleUrls: ['./user-gallery.component.css']
  11. })
  12. export class UserGalleryComponent implements OnInit {
  13.  
  14.   url = 'assets/';
  15.   cars;
  16.   subscriptionCars;
  17.   @Input() userId: number;
  18.  
  19.  
  20.   constructor(private carsService:CarsService) { }
  21.  
  22.   ngOnInit() {
  23.     console.log('ngOnInit');
  24.     this.subscriptionCars = this.carsService.getCars()
  25.       .pipe(
  26.         tap( cars => console.table(cars)),
  27.         filter( car => car.userId == this.userId)
  28.       ).subscribe({
  29.         next:(cars) => {
  30.           console.log('SUBSCRIPTION');
  31.           console.table(cars);
  32.           //this.cars = data.filter( car => car['userId'] == this.userId); <- this code works without pipes
  33.           this.cars =  cars;
  34.         },
  35.         error:error => {
  36.           console.error(error);
  37.         }
  38.       });
  39.   }
  40.  
  41.   ngOnDestroy(){
  42.     if(this.subscriptionCars)
  43.       this.subscriptionCars.unsubscribe();
  44.   }
  45.  
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement