Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

223
Vistas
Angular filter rxjs, it's not filtering correctly

I'm trying to implement the filter operator from rxjs, to filter out different portflios ALL | APP | WEB |API enter image description here

I expect the filter to filter out the portfolios according to the property type: enter image description here

Here is the filter implemmentation

Why does the click not filter portfolios here? what concept I'm I missing?

 portfolios: IPortfolio[] = [];
  portfolioType: 'all' | 'app' | 'web'|'api' = 'all';
  errorMessage = '';
  sub!: Subscription;
   ngOnInit(): void {
    this.sub = this.portFolioService.getPortfolioData().subscribe({
      next: portfolios => {
        this.portfolios = portfolios;
        filter((portfolio: IPortfolio) => portfolio.type === this.portfolioType);
      },
      error: err => this.errorMessage = err
    })
  }

and the view html has

<ul id="portfolio-filters">
  <li [class.filter-active]="portfolioType == 'all'" (click)="portfolioType = 'all'">All</li>
  <li [class.filter-active]="portfolioType == 'app'" (click)="portfolioType = 'app'">APP</li>
  <li [class.filter-active]="portfolioType == 'web'" (click)="portfolioType = 'web'">WEB</li>
  <li [class.filter-active]="portfolioType == 'api'" (click)="portfolioType = 'api'">API</li>
</ul>
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You need to use a .pipe() where you can put a map to transform the portfolios with filter. Placing it as an expression inside the next callback will have no effect.

this.portfolioService.getPortfolioData().pipe(
  map(portfolios => portfolios.filter(portfolio => this.portfolioType === portfolio.type))
).subscribe(portfolios => {
  // portfolios are filtered now
})
about 4 years ago · Juan Pablo Isaza Denunciar

0

As your instance type IPortfolio[] is an array, you should use Array.prototype.filter to filter your portfolios:

 portfolios: IPortfolio[] = [];
  portfolioType: 'all' | 'app' | 'web'|'api' = 'all';
  errorMessage = '';
  sub!: Subscription;
   ngOnInit(): void {
    this.sub = this.portFolioService.getPortfolioData().subscribe({
      next: portfolios => {
        this.portfolios = portfolios.filter(portfolio => portfolio.type === this.portfolioType)
      },
      error: err => this.errorMessage = err
    })
  }

about 4 years ago · Juan Pablo Isaza Denunciar

0

After carefully reading rxjs Documentation, I got the method to best work:

import { Component, OnInit, Injectable } from '@angular/core';
import { BehaviorSubject, Observable, of, Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';
import { IPortfolio } from '../portfolio';
import { PortfolioService } from '../portfolio.service';
@Component({
  selector: 'app-section-portfolio',
  templateUrl: './section-portfolio.component.html',
  styleUrls: ['./section-portfolio.component.scss']
})
export class SectionPortfolioComponent implements OnInit {

  private unsubscribe$: Subject<boolean> = new Subject<boolean>();

  constructor(
    private portfolioService: PortfolioService
  ) { }

  private portfolios: IPortfolio[] = [];
  public portfolios$: BehaviorSubject<Array<IPortfolio>> = new BehaviorSubject<Array<IPortfolio>>([]);
  portfolioType: string = 'all';

  ngOnInit() {
      this.getAllPortfolios();
  }

  private getAllPortfolios () {
      this.portfolioService.getPortfolioData().pipe(
        takeUntil(this.unsubscribe$)
      ).subscribe((portfolios: Array<IPortfolio>) => {
          this.portfolios = portfolios;
          this.portfolios$.next(this.portfolios);
      })
  }

  public applyFilter (filter: string) {
    this.portfolioType = filter
    if (filter === 'all') {
      this.portfolios$.next(this.portfolios);
      return;
    }
    const filteredPortfolios = this.portfolios.filter(portfolio => portfolio.type === filter);
    this.portfolios$.next(filteredPortfolios);
    console.log(this.portfolioType)
  }
}

and in the template

        <ul id="portfolio-filters">
           <li [ngClass]="portfolioType === 'all'? 'filter-active':''"(click)="applyFilter  ('all')">All</li>
          <li [ngClass]="portfolioType === 'app'? 'filter-active':''"  (click)="applyFilter ('app')">APP</li>
          <li [ngClass]="portfolioType === 'web'? 'filter-active':''"  (click)="applyFilter ('web')">WEB</li>
          <li [ngClass]="portfolioType === 'api'? 'filter-active':''" (click)="applyFilter ('api')">API</li>
        </ul>
      </div>
    </div>
      <div *ngFor="let portfolio of (portfolios$|async)">
<!-- End Portfolio Section -->
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda