有没有办法使 datepipe 动态化,以便如果它是美国浏览器,则 datepipe 返回美国格式(yyyy/MM/dd),而当它是欧洲浏览器时,它返回欧洲格式(dd/MM/yyyy)?
Thanks
这可能很困难,尤其是在使用 aot 时。它通常需要您进行不同的构建。我扩展了数据管道并使用浏览器区域设置。
日期管:
@Pipe({name: 'datepipe', pure: true})
export class MyDatePipe extends DatePipe implements PipeTransform {
constructor(private win: WindowRef) {
super(win.ln);
}
transform(value: any, pattern?: string): string | null {
return super.transform(value, pattern);
}
}
Window:
function _window(): any {
// return the global native browser window object
return window;
}
@Injectable()
export class WindowRef {
get nativeWindow(): any {
return _window();
}
public ln = 'en';
constructor() {
try {
if (!isNullOrUndefined(this.nativeWindow.navigator.language) && this.nativeWindow.navigator.language !== '') {
this.ln = this.nativeWindow.navigator.language;
}
}finally {}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)