代码之家  ›  专栏  ›  技术社区  ›  Paco Zevallos

从数组对象筛选*ngFor

  •  -1
  • Paco Zevallos  · 技术社区  · 8 年前

    categoria 是 a . 使用ngFor有什么方法可以实现这一点吗?

    productos: Producto[] = [
      {id:'01', categoria:'a', titulo:'title1'},
      {id:'02', categoria:'b', titulo:'title2'},
      {id:'03', categoria:'a', titulo:'title3'},
      {id:'04', categoria:'c', titulo:'title4'},
      {id:'05', categoria:'c', titulo:'title5'}
    ]
    
    getProductos() {
       return of(this.productos);
    }
    

    组件.ts:

    ngOnInit() {
      this.productos$ = this.ps.getProductos();
    }
    

    组件.html:

    <div *ngFor="let producto of productos$ | async">
      {{ producto.titulo}}
    </div>
    
    2 回复  |  直到 8 年前
        1
  •  1
  •   wFitz    8 年前

    您可以使用管道(请参阅Vinit链接)或过滤器

    getProductos() {
       return of(this.productos.filter( el => el.categoria == 'a'); 
    }
    
        2
  •  0
  •   Ashish Gurjar    8 年前

    您可以使用filter运算符来执行此操作。

    ngOnInit() {
         this.productos$ = this.ps.getProductos().pipe(filter(el => el.categoria == 'a'));
    }
    
    推荐文章