如何基于对象属性字符串过滤"ngFor"循环内的项目

gun*_*dra 11 string filter typescript ngfor angular

我需要ngFor通过更改下拉列表中的类别来过滤循环内的项目.因此,当从列表中选择特定类别时,它应该仅列出包含相同类别的项目.

HTML模板:

<select>
  <option *ngFor="let model of models">{{model.category}}</option>
</select>

<ul class="models">
  <li *ngFor="let model of models" (click)="gotoDetail(model)">
  <img [src]="model.image"/>
  {{model.name}},{{model.category}}
  </li>
</ul>
Run Code Online (Sandbox Code Playgroud)

物品数组:

export var MODELS: Model[] = [
{ id: 1, 
  name: 'Model 1', 
  image: 'img1', 
  category: 'Cat1', 
},

{ id: 2, 
  name: 'Model 2', 
  image: 'img2', 
  category: 'Cat3',
},

{ id: 3, 
  name: 'Model 3', 
  image: 'img3', 
  category: 'Cat1',
},
{ id: 4, 
  name: 'Model 4', 
  image: 'img4', 
  category: 'Cat4',
},

...
];
Run Code Online (Sandbox Code Playgroud)

此外,下拉列表包含重复的类别名称.它必须只列出唯一的类别(字符串).

我知道创建自定义管道是正确的方法,但我不知道如何编写.

Plunker:http://plnkr.co/edit/tpl:2GZg5pLaPWKrsD2JRted?p =preview

Mei*_*eir 19

这是一个示例管道:

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
    name: 'matchesCategory'
})
export class MathcesCategoryPipe implements PipeTransform {
    transform(items: Array<any>, category: string): Array<any> {
        return items.filter(item => item.category === category);
    }
}
Run Code Online (Sandbox Code Playgroud)

要使用它:

<li *ngFor="let model; of models | matchesCategory:model.category" (click)="gotoDetail(model)">
Run Code Online (Sandbox Code Playgroud)

=====对于plunkr示例====

您需要选择更改以反映某些变量

首先在你的班级中定义一个成员:

selectedCategory: string;
Run Code Online (Sandbox Code Playgroud)

然后更新您的模板:

<select (change)="selectedCategory = $event.target.value">
   <option *ngFor="let model of models ">{{model.category}}</option>
</select>
Run Code Online (Sandbox Code Playgroud)

最后,使用管道:

  <li *ngFor="let model; of models | matchesCategory:selectedCategory" (click)="gotoDetail(model)">
Run Code Online (Sandbox Code Playgroud)

====看到羽毛球后的评论====

我注意到你使用了诺言.Angular2更倾向于rxjs.所以我要改变的第一件事是在你的服务中,替换:

getModels(): Promise<Model[]> {
  return Promise.resolve(MODELS);
}
Run Code Online (Sandbox Code Playgroud)

至:

getModels(): Observable<Array<Model>> {
  return Promise.resolve(MODELS);
}
Run Code Online (Sandbox Code Playgroud)

getModels(id: number): Observable<Model> {
  return getModels().map(models => models.find(model.id === id);
}
Run Code Online (Sandbox Code Playgroud)

然后在你的 ModelsComponent

models$: Observable<Array<Model>> = svc.getModels();
uniqueCategories$: Observable<Array<Model>> = this.models$
  .map(models => models.map(model => model.category)
  .map(categories => Array.from(new Set(categories)));
Run Code Online (Sandbox Code Playgroud)

您的选择将成为:

     <option *ngFor="let category; of uniqueCategories$ | async">{{model.category}}</option>
Run Code Online (Sandbox Code Playgroud)

和你的清单:

      <li *ngFor="let model; of models$ | async | matchesCategory:selectedCategory" (click)="gotoDetail(model)">
Run Code Online (Sandbox Code Playgroud)

这是一个非常通风的解决方案,因为您有许多重复项并且您不断查询服务.以此为起点,仅查询服务一次,然后从您获得的结果中获取特定值.

如果您想保留代码,只需实现一个UniqueValuesPipe,它的转换将获得一个参数并过滤它以使用Array.from(new Set(...)).返回唯一类别.您首先需要将其映射到字符串(类别).