2016-12-01 44 views
1

起初:Angular2: custom pipe could not be found這並沒有幫助我在任何情況下.. 我有以下問題:角2自管

管:

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

@Pipe({ 
    name: "ToNumberPipe" 
}) 
export class ToNumberPipe extends DecimalPipe implements PipeTransform { 
    // this allows to pass strings in decimalpipes as well and do nothing if it NAN 
    public transform(value: string, args: string): any { 
     const retNumber = Number(value); 
     if (isNaN(retNumber)) { 
      return value; 
     } 
     return super.transform(value, args); 
    } 
} 

應用:模塊

@NgModule(
    { 
     // all components, pipes and directive which are using this module 
     declarations: [AppComponent, ... all Components go here], 
     // All modules like forms or http 
     imports: [...., MainPipeModule], 
     // all services and other providers 
     providers: [...all services go here], 
     bootstrap: [AppComponent] 
    } 
) 
export class AppModule { 
} 

管模塊

import { NgModule } from "@angular/core"; 
import {CommonModule} from "@angular/common"; 

import {ToNumberPipe} from "./shared/pipes/customNumber.pipe"; 
@NgModule(
    { 
     // all components, pipes and directive which are using this module 
     declarations: [ToNumberPipe], 
     imports: [CommonModule], 
     exports: [ToNumberPipe] 
    }) 
export class MainPipeModule { 
} 

HTML代碼

<span [ngClass]="getClass(rowValue)" class="badge isLink" (click)="selectTask(rowValue.taskId)">{{rowValue.value | ToNumberPipe : "1.0-4"}}</span> 

我試圖直接添加管道到app.module,但同樣的結果...

zone.js:1 Unhandled Promise rejection: Template parse errors: 
The pipe 'ToNumberPipe' could not be found 

我也試着刪除參數,並添加一個簡單的管還有......總是相同的錯誤

角2版本:2.2.4

+0

什麼組件包含使用管道的代碼?這個組件對於什麼'NgModule'屬於? –

+0

所有組件都在同一個app.module文件中。在我將它拆分成一些模塊之前,我認爲它可能是不同模塊的問題。但它並沒有幫助將應用程序的所有部分移動到一個模塊中。 –

回答

1

你必須使用至少v2.3.0-rc.0

在這個版本以下特點包括:core: properly support inheritance

我猜你管被適當地包括等,但這些裝飾(例如:你的名字)沒有發佈正確..

+0

使用2.3.0 rc1可以工作! –

+0

不適用於Angular 2.3.1(+ Webpack) – Tomino

+0

不適合我Angular 4.0.0 =( – Diego