Angular 如何获取筛选列表中的多个项目?

Angular 如何获取筛选列表中的多个项目?,angular,pipe,filtering,Angular,Pipe,Filtering,所以,我有一个过滤管道,用它过滤城市列表,只显示那些包含搜索词的城市 import {Pipe, PipeTransform} from '@angular/core'; @Pipe({ name: 'filter' }) export class FilterPipe implements PipeTransform{ transform(value: any, arg: string) { var searchTerm = arg ?

所以,我有一个过滤管道,用它过滤城市列表,只显示那些包含搜索词的城市

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

    @Pipe({ name: 'filter' })
    export class FilterPipe implements PipeTransform{
        transform(value: any, arg: string) {
            var searchTerm = arg ? arg : '';
            searchTerm = searchTerm.toUpperCase();
            if (value == null) return null;
            return value.filter(el => el.City.toUpperCase().indexOf(searchTerm) !== -1);
        }
    }
但我拥有的城市阵列中有5000个项目,因此我使用切片管仅显示其中的五个项目

<form [formGroup]="form">
  <input formControlName="search" type="text">
  <div>
    <li 
    *ngFor="let city of cities | filter:searchTerm | slice:0:5"
    (click)="PasteCity(city.City)">
      {{ city.City }}
    </li>
  </div>
</form>

  • {{city.city}
  • 但存在一个问题。我需要向用户显示找到了多少个城市,例如“显示了5个631个城市”。 就我所尝试的,
    ViewChildren
    和为
    *ngFor
    方法的最后一个元素编制索引只能得到显示的城市数,而无法通过过滤器找到。。。但是在尝试使用
    ViewChildren
    时,我遇到了一个错误,很快就放弃了这个想法

    你试过了吗

    (cities | filter:searchTerm).length 
    
    你试过了吗

    (cities | filter:searchTerm).length 
    

    你应该使用指令而不是管道我正在试图找到一种方法,但是我找到的所有过滤方法都是使用管道制作的,你能解释一下你建议的方法吗?你应该使用指令而不是管道我正在试图找到一种方法,但是我找到的所有过滤方法都是使用管道制作的,你能解释一下你建议的方法吗?在数组通过切片管道之前,我需要知道长度,所以,正如我侦察到的,你的代码会给我5或更少,而这不是它工作的情况!我在
  • 元素之后添加了“{(cities | filter:searchTerm)].length}}”!非常感谢你!库多西需要在阵列通过切片管道之前知道长度,所以,据我侦察,你的代码会给我5或更少,而这不是它工作的情况!我在
  • 元素之后添加了“{(cities | filter:searchTerm)].length}}”!非常感谢你!荣誉