Short form:
I get an error The pipe 'getListItemText' could not be found
Long form:
I have a need to use a pipe to transform a series of data objects (a mildly complex json object) to an html string
This seems ideal for an angular pipe!
This pipe is very specific to this data, so everything is contained within the component directory:
The pipe (get-list-item-text.pipe.ts
):
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'getListItemText'
})
export class GetListItemTextPipe implements PipeTransform {
transform(value: any, args?: any): any {
return value.trim().split(/\s+/).length;
}
}
(I'll make it more complicated & do what I want once I have the basic thing working)
The component definition (descriptions.module.ts
):
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { DescriptionsComponent } from './descriptions.component';
import { DescriptionResolver } from './description.resolver';
import { DescriptionService } from './description.service';
import { GetListItemTextPipe } from './get-list-item-text.pipe';
@NgModule({
imports: [
CommonModule,
],
declarations: [
DescriptionsComponent,
GetListItemTextPipe,
],
providers: [
DescriptionResolver,
DescriptionService,
]
})
export class DescriptionsModule { }
... and the fragment of html (descriptions.component.html
) I'm working with:
<li *ngFor="let section of notebook.description.sections" ><span \
[innerHTML]="section.prepend | getListItemText "></span></li>
If I run my tests (ng test --watch=false
), I get an error The pipe 'getListItemText' could not be found
:
Chrome Headless 96.0.4664.45 (Linux x86_64) DescriptionsComponent should init, call the DescriptionSelector service, and get a response FAILED
Error: NG0302: The pipe 'getListItemText' could not be found!. Find more at https://angular.io/errors/NG0302
error properties: Object({ code: '302' })
Error: NG0302: The pipe 'getListItemText' could not be found!. Find more at https://angular.io/errors/NG0302
at getPipeDef$1 (node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:25804:1)
at ɵɵpipe (node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:25761:1)
at DescriptionsComponent_section_21_li_7_Template (ng:///DescriptionsComponent.js:7:5)
at executeTemplate (node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:9579:1)
at renderView (node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:9383:1)
at TemplateRef.createEmbeddedView (node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:23036:1)
at ViewContainerRef.createEmbeddedView (node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:23167:1)
at node_modules/@angular/common/__ivy_ngcc__/fesm2015/common.js:3340:1
at DefaultIterableDiffer.forEachOperation (node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:21569:1)
at NgForOf._applyChanges (node_modules/@angular/common/__ivy_ngcc__/fesm2015/common.js:3335:1)
Chrome Headless 96.0.4664.45 (Linux x86_64): Executed 49 of 62 (1 FAILED) (skipped 4) (0 secs / 0.217 secs)
Chrome Headless 96.0.4664.45 (Linux x86_64) DescriptionsComponent should init, call the DescriptionSelector service, and get a response FAILED
Error: NG0302: The pipe 'getListItemText' could not be found!. Find more at https://angular.io/errors/NG0302
error properties: Object({ code: '302' })
Error: NG0302: The pipe 'getListItemText' could not be found!. Find more at https://angular.io/errors/NG0302
at getPipeDef$1 (node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:25804:1)
at ɵɵpipe (node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:25761:1)
at DescriptionsComponent_section_21_li_7_Template (ng:///DescriptionsComponent.js:7:5)
at executeTemplate (node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:9579:1)
at renderView (node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:9383:1)
at TemplateRef.createEmbeddedView (node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:23036:1)
at ViewContainerRef.createEmbeddedView (node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:23167:1)
at node_modules/@angular/common/__ivy_ngcc__/fesm2015/common.js:3340:1
at DefaultIterableDiffer.forEachOperation (node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:21569:1)
If I remove the pipe from the fragment of HTML, the tests pass fine.
Everything I've found implies this is a correct implementation, however it's obviously not.
What have I missed?
How do I specify, and then use, a custom pipe?
(just to note, I did try a top-level definition, and adding the pipe definition to the app.module.ts
file.... same problem; and adding a constructor
as suggected in Getting 'Could Not Be Found' Error re: Custom Pipe in Angular 2 App makes no difference)