【问题标题】:After creating custom Search Pipe to Dynamically Filter results , i am still getting Failed: parse errors: The pipe 'filter' could not be found创建自定义搜索管道以动态过滤结果后,我仍然失败:解析错误:找不到管道“过滤器”
【发布时间】:2018-04-28 21:24:01
【问题描述】:

我正在 angular4 和 node.js 中部署一个项目。我创建了一个名为 filter.pipe.js 的自定义管道过滤器文件。以及所有相关的代码。我仍然收到错误-->

Failed: Template parse errors:
The pipe 'filter' could not be found ("
  </thead>
  <tbody>
    <tr *ngFor="let[ERROR ->] dat of result | filter:filterdata| 
 paginate: { itemsPerPage: 5, currentPage: p };let i = index ">
  "): ng:///DynamicTestModule/TransactionComponent.html@220:23

代码

filter.pipe.ts

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


 @Pipe({
   name: 'filter'
  })

 export class FilterPipe implements PipeTransform {
 transform(items: any[], filterdata: string): any[] {
 if(!items) return [];
 if(!filterdata) return items;
  filterdata = filterdata.toLowerCase();
  return items.filter( it => {
  return it.toLowerCase().includes(filterdata);
   });
   }
  }

app.module.ts

 import { FilterPipe} from './transaction/filter.pipe';
 import { Pipe, PipeTransform } from '@angular/core';


 @NgModule({
 imports: [ ..],

   declarations: [ FilterPipe,..]
  )}

app.component.ts

 import { Pipe, PipeTransform } from '@angular/core';
 import { FilterPipe} from '../transaction/filter.pipe';

app.component.html

   <td style="width:50%">
              <input class="form-control" id="input1-group1 " 
  style="margin-top:20px" type="text" name="search" placeholder="Enter 
  Search Text"
  [(ngModel)]="filterdata">

 </td>

  <tr *ngFor="let dat of result | filter:filterdata| paginate: { 
    itemsPerPage: 5, currentPage: p };let i = index ">

 //some more html

请帮忙。

【问题讨论】:

  • 欢迎来到 Stack Overflow。您附加到问题的“标签”(通常在您键入问题时选择)旨在对您的问题所涉及的主题进行分类。如前所述,这些确实会在您键入某些“术语”时自动填充,但这并不总是完全准确地说明您的问题。在添加新问题时请注意这些。然后确保唯一选择的“标签”是那些与您实际提出的问题“直接”相关的标签。应该删除不属于问题代码的“堆栈”的其他组件。
  • 女士,您可以显示结果响应
  • 结果出现在控制台上。但它并没有像它说的那样显示。错误类型错误:it.toLowerCase 不是 Object.eval 的 FilterPipe.transform (filter.pipe.ts:24) 的 Array.filter () 的 eval (filter.pipe.ts:25) 的函数 [as updateDirectives ] (TransactionComponent.html:221) 在 Object.debugUpdateDirectives [as updateDirectives] (core.es5.js:13058)
  • 删除 .to.toLowerCase 后我得到错误 -->ERROR TypeError: it.includes is not a function at eval (filter.pipe.ts:25)
  • "it" 是对象而不是字符串,首先检查 "it" 中的值

标签: javascript angular


【解决方案1】:

这是我的示例应用程序,您可以在 stackblitz 上对其进行测试 stackblitz

app.module.ts

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
import { HelloComponent } from './hello.component';
import { FilterPipe} from './filter.pipe';
import { Pipe, PipeTransform } from '@angular/core';

@NgModule({
  imports:      [ BrowserModule, FormsModule ],
  declarations: [ AppComponent, HelloComponent, FilterPipe ],
  bootstrap:    [ AppComponent ]
})
export class AppModule { }

app.component.ts

        import { Component } from '@angular/core';
        import { Pipe, PipeTransform } from '@angular/core';
        import { FilterPipe} from './filter.pipe';
        @Component({
         selector: 'my-app',
         templateUrl: './app.component.html',
         styleUrls: [ './app.component.css' ]
        })
        export class AppComponent  {
         name = 'Angular 5';
         public filterdata = null;

         public result = [
            { id: 1, name: 'Vikram'},
            { id: 2, name: 'Vivek'},
            { id: 3, name: 'Vijay'}
         ]
        }

filter.pipe.ts

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


        @Pipe({
         name: 'filter'
        })

        export class FilterPipe implements PipeTransform {
         transform(items: any[], filterdata: string): any[] {
         if(!items) return [];
         if(!filterdata) return items;
          console.log(filterdata);
          filterdata = filterdata.toString().toLowerCase();
          return items.filter( it => {
            console.log(it);
            return it.name.toLowerCase().includes(filterdata);
           });
         }
        }

app.component.html

    <input class="form-control" id="input1-group1 " 
    style="margin-top:20px" type="text" name="search" placeholder="Enter 
    Search Text"
    [(ngModel)]="filterdata">

    <table border="1">
    <thead>
        <th>Id</th> <th>Name</th>
    </thead>
    <tbody>
    <tr *ngFor="let data of result | filter:filterdata ">
    <td>{{data.id}}</td> <td>{{data.name}}</td>
    </tr>  
    </tbody>

    </table>  

【讨论】:

  • 这里 vikram 我有一个问题。在这段代码中 --> return it.name.toLowerCase().includes(filterdata); ..它专门用于名称...我想要所有列的选项。 ..错误就在那里 ..ERROR TypeError: it.toLowerCase is not a function 在删除 .toLowerCase() ...它给出错误 ERROR TypeError: it.includes is not a function at eval (filter.pipe .ts:25)
  • 好的女士,您能否为您的列名提供值。我很容易制作
  • 如果你使用 angular material.io stackblitz.com/angular/klbqqvjjgjv,你可以使用它
  • 感谢 vikram... 但有超过 15 列。我可能无法添加它们。谢谢到目前为止。但是你需要列名...你会在 --> return items.filter(it => { console.log(it); return it.name.toLowerCase().includes(filterdata); 中包含各个列名吗?
  • 嗨 vikram,你能帮帮我吗stackoverflow.com/questions/50095904/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-07-01
  • 2019-07-04
  • 2019-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-25
相关资源
最近更新 更多