2016-10-04 47 views
1

我有一个类里面,而我想用日期过滤,因为它曾经是角1:如何实现日期滤波Angular2

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

现在我想要实现这个采用了棱角分明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

你加'CommonModule'或'BrowserModule'的成分,你正在使用'DatePipe'?您可能需要在'@NgModule({providers:[DatePipe],...})中明确提供''DatePipe'也不是过滤日期,而是格式化日期。 –

+0

@GünterZöchbauer不,我还没有添加它。 – user2128702

+0

@GünterZöchbauer当我尝试将它添加到我的NgModule中时,它变为红色,并显示相同的消息。我是否需要导入额外的东西? PS:抱歉的跛脚问题,但我是完全新的角 – user2128702

回答

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

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