Set an AngularJS date filter format to be used by all references to the date filter?

前端 未结 5 969
余生分开走
余生分开走 2021-02-18 18:03

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\'

5条回答
  •  余生分开走
    2021-02-18 18:19

    Use a decorator,

    It's not what you explicitly asked for, use a decorator. it's built in angular and designed to patch services, directives, filters etc ...

    in your case, if the date filter is not provided, you should use your custom date format.

    app.config(function ($provide) {
        $provide.decorator('dateFilter', function ($delegate) {
            return function () {
    
                // Check if the date format argument is not provided
                if (!arguments[1]) { 
                    arguments[1] = 'dd MMMM @ HH:mm:ss';
                }
    
                var value = $delegate.apply(null, arguments);
                return value;
            };
        })
    });
    

提交回复
热议问题