在AngularJS中,我能够使用类似于此的语法在服务和控制器内部使用过滤器(管道):
$filter('date')(myDate, 'yyyy-MM-dd');
Run Code Online (Sandbox Code Playgroud)
是否可以在Angular中使用像这样的服务/组件中的管道?
cex*_*yat 602
像往常一样在Angular中,您可以依赖依赖注入:
import { DatePipe } from '@angular/common';
class MyService {
constructor(private datePipe: DatePipe) {}
transformDate(date) {
return this.datePipe.transform(date, 'yyyy-MM-dd');
}
}
Run Code Online (Sandbox Code Playgroud)
添加DatePipe到模块中的提供者列表中; 如果你忘记这样做,你会收到一个错误no provider for DatePipe:
providers: [DatePipe,...]
Run Code Online (Sandbox Code Playgroud)
更新Angular 6:Angular 6现在公开提供管道使用的几乎所有格式化功能.例如,您现在可以formatDate直接使用该功能.
import { formatDate } from '@angular/common';
class MyService {
constructor(@Inject(LOCALE_ID) private locale: string) {}
transformDate(date) {
return formatDate(date, 'yyyy-MM-dd', this.locale);
}
}
Run Code Online (Sandbox Code Playgroud)
在Angular 5之前:请注意,DatePipe直到版本5依赖于Intl API,并不是所有浏览器都支持(检查兼容性表).
如果您使用的是较旧的Angular版本,则应将Intlpolyfill 添加到项目中以避免任何问题.有关更详细的答案,请参阅此相关问题.
Sna*_*ops 70
建议使用其他答案的DI方法而不是这种方法
您应该可以直接使用该类
new DatePipe().transform(myDate, 'yyyy-MM-dd');
Run Code Online (Sandbox Code Playgroud)
例如
var raw = new Date(2015, 1, 12);
var formatted = new DatePipe().transform(raw, 'yyyy-MM-dd');
expect(formatted).toEqual('2015-02-12');
Run Code Online (Sandbox Code Playgroud)
Pra*_*obh 14
是的,可以使用简单的自定义管道.使用自定义管道的好处是,如果我们将来需要更新日期格式,我们可以更新单个文件.
import { Pipe, PipeTransform } from '@angular/core';
import { DatePipe } from '@angular/common';
@Pipe({
name: 'dateFormatPipe',
})
export class dateFormatPipe implements PipeTransform {
transform(value: string) {
var datePipe = new DatePipe("en-US");
value = datePipe.transform(value, 'MMM-dd-yyyy');
return value;
}
}
{{currentDate | dateFormatPipe }}
Run Code Online (Sandbox Code Playgroud)
您可以随时随地使用此管道,组件,服务等
例如
export class AppComponent {
currentDate : any;
newDate : any;
constructor(){
this.currentDate = new Date().getTime();
let dateFormatPipeFilter = new dateFormatPipe();
this.newDate = dateFormatPipeFilter.transform(this.currentDate);
console.log(this.newDate);
}
Run Code Online (Sandbox Code Playgroud)
别忘了导入依赖项.
import { Component } from '@angular/core';
import {dateFormatPipe} from './pipes'
Run Code Online (Sandbox Code Playgroud)
csg*_*000 12
我收到一个错误,因为DatePipe不是提供者,因此无法注入.一种解决方案是将其作为应用程序模块中的提供程序,但我首选的解决方案是实例化它.
我查看了DatePipe的源代码,看看它是如何获得语言环境的:https://github.com/angular/angular/blob/5.2.5/packages/common/src/pipes/date_pipe.ts#L15-L174
我想在管道中使用它,所以我的例子在另一个管道中:
import { Pipe, PipeTransform, Inject, LOCALE_ID } from '@angular/core';
import { DatePipe } from '@angular/common';
@Pipe({
name: 'when',
})
export class WhenPipe implements PipeTransform {
static today = new Date((new Date).toDateString().split(' ').slice(1).join(' '));
datePipe: DatePipe;
constructor(@Inject(LOCALE_ID) private locale: string) {
this.datePipe = new DatePipe(locale);
}
transform(value: string | Date): string {
if (typeof(value) === 'string')
value = new Date(value);
return this.datePipe.transform(value, value < WhenPipe.today ? 'MMM d': 'shortTime')
}
}
Run Code Online (Sandbox Code Playgroud)
这里的关键是从angular的核心导入Inject和LOCALE_ID,然后注入它,这样你就可以将它提供给DatePipe来正确实例化它.
在您的应用模块中,您还可以将DatePipe添加到您的提供者数组,如下所示:
import { DatePipe } from '@angular/common';
@NgModule({
providers: [
DatePipe
]
})
Run Code Online (Sandbox Code Playgroud)
现在你可以在你需要的构造函数中注入它(比如在cexbrayat的答案中).
两种解决方案都有效,我不知道哪一个角度会考虑最"正确",但我选择手动实例化它,因为角度没有提供datepipe作为提供者本身.
从Angular 6开始,您可以formatDate从@angular/common实用程序导入以在组件内部使用.
它是在https://github.com/smdunn/angular/commit/3adeb0d96344c15201f7f1a0fae7e533a408e4ae上引入的
我可以用作:
import {formatDate} from '@angular/common';
formatDate(new Date(), 'd MMM yy HH:mm', 'en');
Run Code Online (Sandbox Code Playgroud)
虽然必须提供区域设置
您可以使用 formatDate() 来格式化服务或组件 ts 中的日期。句法:-
formatDate(value: string | number | Date, format: string, locale: string, timezone?: string): string
Run Code Online (Sandbox Code Playgroud)
像这样从公共模块导入 formatDate(),
import { formatDate } from '@angular/common';
Run Code Online (Sandbox Code Playgroud)
并像这样在课堂上使用它,
formatDate(new Date(), 'MMMM dd yyyy', 'en');
Run Code Online (Sandbox Code Playgroud)
您还可以像这样使用 angular 提供的预定义格式选项,
formatDate(new Date(), 'shortDate', 'en');
Run Code Online (Sandbox Code Playgroud)
您可以在此处查看所有其他预定义格式选项,
如果您不想执行'new myPipe()',因为您正在向管道注入依赖项,您可以注入像提供程序这样的组件,并在没有新的情况下使用.
例:
// In your component...
import { Component, OnInit } from '@angular/core';
import { myPipe} from './pipes';
@Component({
selector: 'my-component',
template: '{{ data }}',
providers: [ myPipe ]
})
export class MyComponent() implements OnInit {
data = 'some data';
constructor(private myPipe: myPipe) {}
ngOnInit() {
this.data = this.myPipe.transform(this.data);
}
}
Run Code Online (Sandbox Code Playgroud)
小智 6
如果要在组件中使用自定义管道,可以添加
@Injectable({
providedIn: 'root'
})
Run Code Online (Sandbox Code Playgroud)
自定义管道的注释.然后,您可以将其用作服务
| 归档时间: |
|
| 查看次数: |
205229 次 |
| 最近记录: |