设置为将日期过滤器的所有引用中使用的AngularJS日期过滤器格式? [英] Set an AngularJS date filter format to be used by all references to the date filter?

查看:112
本文介绍了设置为将日期过滤器的所有引用中使用的AngularJS日期过滤器格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

宁可不必定义自定义格式每次调用日期过滤器,有没有办法定义全局的默认格式(比中)?

Rather that having to define a custom format for each call to the date filter, is there a way to globally define a default format (other than 'medium')?

我想的格式在一个文件中,而不是全在我的code处设置(日期格式最终可能在未来和在一个文件的修改被更改会比其好得多使许多文件的更改)。

I would like to have the format set in one file rather than all over the place in my code (the date format may end up being changed in the future and a modification in one file would be much nicer than having to make changes in many files).

这是我在做什么现在(定义日期格式的每一次):

This is what I am doing now (defining date format each time):

{{systemTime | date:'dd MMMM @ HH:mm:ss'}}
{{modifiedTime | date:'dd MMMM @ HH:mm:ss'}}
{{getShippedTime() | date:'dd MMMM @ HH:mm:ss'}}
   etc.

我想创建一个自定义过滤器(我们称之为数值指明MyDate ),这将作为一个包装,然后通过关闭日期字符串的角日期过滤与我的自定义格式,所以后来我的code可能只是看起来像这样:

I was thinking of creating a custom filter (let's call it myDate), which would act as a wrapper and then pass off the date string to the Angular date filter with my custom format, so then my code could just look like this:

{{systemTime | myDate}}
{{modifiedTime | myDate}}
{{getShippedTime() | myDate}}
   etc.

不过,我无法弄清楚如何使数值指明MyDate 过滤器指向角日期过滤器。

However, I can't figure out how to make the myDate filter point to the Angular date filter.

感谢您的帮助。

推荐答案

根据一些调查研究,然后考虑通过moderndegree的评论,我发现下面的数值指明MyDate 过滤器将工作:

Based on some more research and then considering the comment by moderndegree, I have found that the following myDate filter will work:

.filter('myDate', function($filter) {    
    var angularDateFilter = $filter('date');
    return function(theDate) {
       return angularDateFilter(theDate, 'dd MMMM @ HH:mm:ss');
    }
});

这篇关于设置为将日期过滤器的所有引用中使用的AngularJS日期过滤器格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆