Angular 2 导入 DatePipe 和转换不工作

Angular 2 import DatePipe and transform not working

我正在使用 Angular 2 获取异常免费日期,在这种情况下,我从服务器获取“0000-00-00”日期并抛出异常,所以我按照 Pipe 进行过滤。

{{"2016-05-11" | dateFormat:task_.date_format}} 

管道代码 -

import { Pipe, PipeTransform } from '@angular/core';
import { DatePipe } from '@angular/common';
import { TaskService } from '../providers/taskService';

@Pipe({name: 'dateFormat'})
export class DateFormatPipe implements PipeTransform{


    constructor(public taskService: TaskService,private datePipe: DatePipe){}

    transform(value :any[],arg:any): any  {

        console.log("dateFormat value "+value+"\n arg "+arg);

        let format_ = this.taskService.getDateFormate(arg);
        console.log("format_ ",format_)
        let formatted = this.datePipe.transform(value, format_);
        console.log("formatted "+formatted);
        // if (value == this.taskService.getDateFormate(arg)) {
        return value;
        // }
    }
}

但它不起作用,我只希望如果日期正确,则选择 return,即 yyyy-mm-dd、mm-dd-yyyy 等等,否则 return 0000-00 -00.

您是否将管道包含在 app.module 文件(声明数组)的 bootstrap 中?

然后检查你的语句中的错字(猜测 Formate 应该是 Format) 让 format_ = this.taskService.getDateFormate(arg);

除此之外,您是否知道 Angular2 具有 it's own implementation of the Date pipe? 此外,如文档中所述,如果您使用的是 Safari 或更旧的浏览器,则必须包含 ECMAScript 国际化 API

请检查您的变量命名约定(我认为 task_ 看起来不太好...)

给变量类型如

datePipeEn: DatePipe = new DatePipe('en-US');

并且不要忘记'en-Us'然后使用转换方法的语言代码。