在Angular 2中,为了本地化日期管道,您需要提供LOCALE_ID
。我有一个公开locale$: Observable<string>
的服务LocaleService
,它实现了BehaviorSubject<string>
。我不想给这个服务添加任何状态,我希望它是完全被动的。如何在提供LOCALE_ID
时从该服务获取信息
发布于 2017-08-15 07:44:08
如果你想为你的应用程序设置一次语言,LOCALE_ID的解决方案是很棒的。但是如果你想在运行时改变语言,它就不起作用了。请参阅我的答案here。
发布于 2017-02-03 14:53:09
管道可以返回一个可观察对象。
import { Pipe, PipeTransform } from '@angular/core';
import { LocaleService } from 'shared/locale.service';
import { Observable } from 'rxjs/Observable';
import { formatDate } from 'somelibrary';
import 'rxjs/add/operator/map';
@Pipe({
name: 'myDate',
pure: false
})
export class MyDatePipe implements PipeTransform {
constructor(private localeService: LocaleService) { }
transform(date: string): Observable<String> {
return this.localeService.locale$.map(locale => formatDate(date, locale));
}
}
https://stackoverflow.com/questions/42026342
复制相似问题