Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 412
Ivan Avatar asked Oct 31 '16 11:10

Ivan


1 Answers

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.

like image 109
Günter Zöchbauer Avatar answered Sep 27 '22 21:09

Günter Zöchbauer