如何在Angular2中实现日期过滤

时间:2016-10-04 07:23:27

标签: angularjs angular date-format

我有一个类,其中我想使用日期过滤,因为它曾经在Angular 1中:

$filter('date')(startDate, 'yyyy-MM-dd HH:mm:ss')

现在我想使用Angular 2来实现这一点。据我所见,我可以使用DatePipe类。但问题是我不知道如何在我的类文件中导入它:

import { Injectable } from '@angular/core'
import { Http, Response, Headers } from '@angular/http';
import { InputValidatorService }  from './input-validator.service';
import { Pipe, PipeTransform } from '@angular/core';

...

myFunctionInsideOfAClass(){
   var datePipe = new DatePipe(); // Symbol 'DatePipe' can not be properly resolved, probably it is located in inaccessible module.
}

我怎样才能做到这一点?

编辑:我的应用模块:

import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule }   from '@angular/forms';

..
@NgModule({
    imports: [BrowserModule, FormsModule, HttpModule],
    declarations: [AppComponent, MapComponent, AtileLayerDirective, MapDirective, mapControlDirective, ToolsComponent, SearchComponent],
    bootstrap: [AppComponent],
    providers: [{
        provide: Http,
        useFactory: (_backend: ConnectionBackend, _defaultOptions: RequestOptions) => new HttpInterceptor(_backend, _defaultOptions),
        deps: [XHRBackend, RequestOptions]
    }, mapManager, SearchService, StatusIconProvider
    ]
})

1 个答案:

答案 0 :(得分:1)

import {DatePipe} from '@angular/common';

@NgModule({
  ...
  providers: [ /* other providers */, DatePipe],
  ...
})