当前位置: 首页 > 知识库问答 >
问题:

找不到管道“”angular2自定义管道

周云
2023-03-14
import { Pipe, PipeTransform } from '@angular/core';

import { User } from '../user/user';

@Pipe({
  name: 'usersPipe',
  pure: false
})
export class UsersPipe implements PipeTransform {
  transform(users: User [], searchTerm: string) {
    return users.filter(user => user.name.indexOf(searchTerm) !== -1);
  }
}
<input [(ngModel)]="searchTerm" type="text" placeholder="Search users">

<div *ngFor="let user of (users | usersPipe:searchTerm)">
...
</div>

错误:

zone.js:478 Unhandled Promise rejection: Template parse errors:
The pipe 'usersPipe' could not be found ("
<div class="row">
    <div  
    [ERROR ->]*ngFor="let user of (user | usersPipe:searchTerm)">

棱角版本:

"@angular/common": "2.0.0-rc.5",
"@angular/compiler": "2.0.0-rc.5",
"@angular/core": "2.0.0-rc.5",
"@angular/platform-browser": "2.0.0-rc.5",
"@angular/platform-browser-dynamic": "2.0.0-rc.5",
"@angular/router": "3.0.0-rc.1",
"@angular/forms": "0.3.0",
"@angular/http": "2.0.0-rc.5",
"es6-shim": "^0.35.0",
"reflect-metadata": "0.1.3",
"rxjs": "5.0.0-beta.6",
"systemjs": "0.19.26",
"bootstrap": "^3.3.6",
"zone.js": "^0.6.12"

共有1个答案

施彬郁
2023-03-14

确保您没有面临“跨模块”问题

如果使用管道的组件不属于“全局”声明管道组件的模块,则找不到管道,您将得到此错误消息。

在我的例子中,我在一个单独的模块中声明了管道,并将这个管道模块导入到任何其他具有使用管道的组件的模块中。

 import { NgModule }      from '@angular/core';
 import { myDateFormat }          from '../directives/myDateFormat';

 @NgModule({
     imports:        [],
     declarations:   [myDateFormat],
     exports:        [myDateFormat],
 })

 export class PipeModule {

   static forRoot() {
      return {
          ngModule: PipeModule,
          providers: [],
      };
   }
 } 
  // Import APPLICATION MODULES
  ...
  import { PipeModule }    from './tools/PipeModule';

  @NgModule({
     imports: [
    ...
    , PipeModule.forRoot()
    ....
  ],
 类似资料:
  • 使装饰器包含具有name属性的管道元数据。 此值将用于在模板表达式中调用此管道。 它必须是有效的JavaScript标识符。 实现PipeTransform接口的transform方法。 此方法接受管道的值和任何类型的可变数量的参数,并返回一个变换的(“管道”)值。 import { Component } from '@angular/core'; selector: 'app-root',

  • 即。在货币管道上完成一些额外的格式化。为此,我想在自定义管道的组件代码中使用现有管道。

  • Angular 2还具有创建自定义管道的功能。 定义自定义管道的一般方法如下。 import { Pipe, PipeTransform } from '@angular/core'; @Pipe({name: 'Pipename'}) export class Pipeclass implements PipeTransform { transform(parameters): r

  • 本文向大家介绍Angular 2 创建自定义管道,包括了Angular 2 创建自定义管道的使用技巧和注意事项,需要的朋友参考一下 示例 app / pipes.pipe.ts app / my-component.component.ts            

  • 获取频道自定义菜单 删除频道自定义菜单 设置频道自定义菜单

  • 我正在使用一个自定义管道,过滤数组的第一个字母表在字符串的基础上数组(电话簿样式)。每当我更改字母表参数时,管道就会返回经过过滤的数组,并使用*ngfor显示它。如果没有找到匹配项,则返回空数组,并且我的diplay are为空。 我希望,如果管道返回空数组,我的显示区域应该显示一个div'没有记录找到‘。我怎么能那么做。