简体   繁体   English

将Angular过滤器与另一个过滤器传递的参数一起应用

[英]Apply Angular filter with an argument passed by another filter

I need to use the date filter with a format created by another filter. 我需要使用其他过滤器创建的格式的日期过滤器。 Trying to combine the date filter with the angular-translate filter. 尝试将date过滤器与angular-translate过滤器结合使用。

With a hardcoded format I would have: 使用硬编码格式,我将拥有:

{{foo | date:'yyyy-MM-dd'}}

What I need is to get the date format by another filter. 我需要的是通过另一个过滤器获取日期格式。

Something like this: 像这样:

{{foo | date:$filter('translate')('global.dateFormatNoTime')}}

or eventually this: 或者最终是这样:

{{foo | date:('yyyy-MM-dd' | dateFormat)}}

'yyyy-MM-dd' being default format if the dateFormat filter not returning anything. 如果dateFormat过滤器未返回任何内容,则'yyyy-MM-dd'为默认格式。

Right now I do this using a scope variable with the second filter applied on it in the controller. 现在,我使用范围变量,并在控制器中应用了第二个过滤器来执行此操作。

{{foo | date:dateFormat}}

but wondering if can this be done directly in the view without another scope variable. 但是想知道是否可以在没有另一个范围变量的情况下直接在视图中完成此操作。

.filter('myCustomerFilter', function ($filter) {
    return function (date) {
        var dateFormat = $filter('translate')('global.dateFormatNoTime');
        return $filter('date')(date, dateFormat);
    }
});

And you can use this custom filter in your view. 您可以在视图中使用此自定义过滤器。

{{foo | myCustomerFilter}}

Or if you want to pass the translate parameter into your filter you can define your custom filter like the below, 或者,如果您要将translate参数传递到过滤器中,则可以定义自定义过滤器,如下所示,

.filter('myCustomerFilter', function ($filter) {
    return function (input, date, translateOption) {
        var dateFormat = $filter('translate')(translateOption);
        return $filter('date')(dateFormat);
    }
});

And in your view you can pass the translate parameter into your custom filter. 并且在您看来,您可以将translate参数传递到自定义过滤器中。

{{foo | myCustomerFilter : 'global.dateFormatNoTime'}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM