I can't seem to fix this error. I have a search bar and an ngFor. I am trying to filter the array using a custom pipe like this:
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);
}
}
Usage:
<input [(ngModel)]="searchTerm" type="text" placeholder="Search users">
<div *ngFor="let user of (users | usersPipe:searchTerm)">
...
</div>
Error:
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 versions:
"@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"
I have created a module for pipes in the same directory where my pipes are present
Now import that module in app.module:
Now it can be used by importing the same in the nested module
I found the "cross module" answer above very helpful to my situation, but would want to expand on that, as there is another wrinkle to consider. If you have a submodule, it also can't see the pipes in the parent module in my testing. For that reason also, you may need to put pipes into there own separate module.
Here's a summary of the steps I took to address pipes not being visible in the submodule:
Another footnote to the above "cross module" answer: when I created the PipeModule I removed the forRoot static method and imported PipeModule without that in my shared module. My basic understanding is that forRoot is useful for scenarios like singletons, which don't apply to filters necessarily.
Make sure you are not facing a "cross module" problem
If the component which is using the pipe, doesn't belong to the module which has declared the pipe component "globally" then the pipe is not found and you get this error message.
In my case I've declared the pipe in a separate module and imported this pipe module in any other module having components using the pipe.
I have declared a that the component in which you are using the pipe is
the Pipe Module
Usage in another module (e.g. app.module)
You need to include your pipe in module declaration:
Note : Only if you are not using angular modules
For some reason this is not in the docs but I had to import the custom pipe in the component
For Ionic you can face multiple issues as @Karl mentioned. The solution which works flawlessly for ionic lazy loaded pages is:
// pipes.ts content (it can have multiple pipes inside, just remember to
// pipes.module.ts content
Include PipesModule into app.module and @NgModule imports section
import { PipesModule } from "../pipes/pipes.module"; @NgModule({ imports: [ PipesModule ] });
Include PipesModule in each of your .module.ts where you want to use custom pipes. Don't forget to add it into imports section. // Example. file: pages/my-custom-page/my-custom-page.module.ts
import { PipesModule } from "../../pipes/pipes.module"; @NgModule({ imports: [ PipesModule ] })
Thats it. Now you can use your custom pipe in your template. Ex.
<div *ngFor="let prop of myObject | toArray">{{ prop.key }}</div>