【问题标题】:Angular test failing because of Custom Filter由于自定义过滤器,角度测试失败
【发布时间】:2017-11-09 11:14:06
【问题描述】:

我在表格的搜索栏中使用管道。我收到以下错误:

      The pipe 'filter' could not be found ("
        <tbody>
        <tr *ngFor="l[ERROR ->]et i of tutors | filter : searchText" name="tutors_list">
          <td>{{i.name}}</td>

我在app.module.ts的声明中添加了过滤器。

这是我的自定义过滤器:

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

@Pipe({
  name: 'filter'
})
export class FilterPipe implements PipeTransform {

  transform(items: any[], searchText: string) {
   if (searchText === undefined) return items;

   return items.filter(function(i){
    if( i.name.toLowerCase().includes(searchText.toLowerCase()))
    {  return i;}
    else if( i.department.toLowerCase().includes(searchText.toLowerCase()))
    {  return i;}
    else if( i.email.toLowerCase().includes(searchText.toLowerCase()))
    {  return i;}
   });

  }

}

【问题讨论】:

    标签: angular angular2-routing


    【解决方案1】:

    在您的 App 模块中,将您的 FilterPipe 类包含在 declarations 数组中。

    // App.module.ts
    @NgModule({
      declarations: [
        ...,
        FilterPipe // This i what you are missing.
      ],
    ...
    export class AppModule {
       ...
    }
    

    对于单元测试,这必须在 TestBed 中声明。类似的,

    ...
    
        TestBed.configureTestingModule({
          declarations: [ FilterPipe ], // Declare your filter here
                  ...
        });
    
    ...
    

    【讨论】:

      猜你喜欢
      • 2019-04-16
      • 1970-01-01
      • 2021-09-18
      • 2020-07-27
      • 2019-02-16
      • 2015-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多