Ivan
Ivan

Reputation: 895

Angular2.0.0 pipe '' could not be found

Error:

Error: Template parse errors: The pipe 'datefromiso' could not be found

Pipe:

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

    @Pipe({
        name: 'datefromiso'
    })
    export class DateFromISO implements PipeTransform {
        transform(value: any, args: string[]): string {
            if (value) {
                var date = value instanceof Date ? value : new Date(value);
                return date.getDate() + '/' + (date.getMonth()+1) + '/' + (date.getYear()+1900);
            }
        }
    }

App module:

import { DateFromISO } from './pipes/date-from-iso';
...
@NgModule({
  bootstrap: [ App ],
  declarations: [
    App,
    ErrorComponent,
    DateFromISO
  ]

HTML:

<div class="pull-right">{{entity.ts | datefromiso}}</div>

entity.ts is ISO string. What's wrong? Also a question: if there are better way to transform ISO string to locale date in html with angular2? Thanks in advance.

Upvotes: 6

Views: 8543

Answers (1)

G&#252;nter Z&#246;chbauer
G&#252;nter Z&#246;chbauer

Reputation: 657088

You need to add the module that contains DateFromISO to imports of every module where you use it.

It's therefore advisable to create a feature module that contains the pipe and perhaps other reusable directives and components and then add this module to imports in all modules where they should be available.

The pipe and other reusable components and directives need to be added to declarations and exports in this feature module.

Upvotes: 9

Related Questions