1
resposta

Aplicar o filtro usando o ngOnChanges

Pq nao funcionaria da maneira abaixo?

import { Component, OnInit, OnChanges, SimpleChanges } from '@angular/core';
import { PhotoService } from '../photo/photo.service';
import { ActivatedRoute } from '@angular/router';
import { Photo } from '../photo/photo';

@Component({
  selector: 'ap-photo-list',
  templateUrl: './photo-list.component.html',
  styleUrls: ['./photo-list.component.css']
})
export class PhotoListComponent implements OnInit, OnChanges {

  photos: Photo[] = [];
  filter: string = "";

  constructor(
    private photoService: PhotoService,
    private activatedRoute: ActivatedRoute) {  }



  ngOnInit(): void{
    const userName = this.activatedRoute.snapshot.params.userName;
    this.photoService.ListFromUser(userName).subscribe(
      photos => this.photos = photos)
      console.log("Starting")
  }

  ngOnChanges(changes: SimpleChanges)
  {
    if(changes.filter)
      this.photos = this.applyFilter(this.filter);
  }

  applyFilter(filter: string): Photo[] {
    const newPhotos = [];
    for(let index=0;index<this.photos.length;index+=1){
        if(this.photos[index].description.indexOf(filter)>0){
          newPhotos.push(this.photos[index]);
        }
    }
    return newPhotos;
  }
}
1 resposta

Fala ai Marcos, tudo bem? Desculpa mas não peguei a diferença, faz muito tempo que fiz o curso, consegue me passar mais detalhes por favor?

Fico no aguardo.