找不到Angular 2自定义管道

时间:2016-12-01 15:35:36

标签: angular

起初:Angular2: custom pipe could not be found在任何情况下这对我都没有帮助.. 我有以下问题:

管:

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>

我尝试将Pipe直接添加到app.module,但结果相同......

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

此外,我尝试删除参数并添加一个简单的管道...总是相同的错误

Angular 2版本:2.2.4

1 个答案:

答案 0 :(得分:1)

您必须至少使用v2.3.0-rc.0

此版本包含以下功能:core: properly support inheritance

我猜你的管道已正确包含等等,但那些装饰器(例如你的名字)没有正确发布..