注射dateFilter在AngularJs服务 [英] Inject dateFilter in a service in AngularJs

查看:101
本文介绍了注射dateFilter在AngularJs服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有在AngularJs服务注入过滤器的方法。

I would like to know if there is a way to inject the filters in a service in AngularJs.

我一直在试图

app.factory('educationService', [function($rootScope, $filter) {

    // ..... Some code

    // What I want
    console.log(dateFilter(new Date(), 'yyyy-MM-01'));

    // ..... Some code

}]);

所以,我想知道是否有可能注入在服务过滤器或也许是另一种方式访问​​。

So I would like to know if it is possible to inject a filter in a service or maybe it is accessible in another way.

如果你有一个链接到这点的文档,这将是非常好的:)在角商务部一直在寻找,我发现没有谈到这个真的很有帮助。

And if you have a link to a documentation about this point, it would be really nice :) Been searching in the doc of Angular and I found nothing really helpful about this.

感谢:)

推荐答案

首先,你必须注入过滤器是这样的:

First you have to inject the filter like this:

app.factory('myService', ['$rootScope', '$filter', function($rootScope, $filter) 

(当你在构建过程中使用缩小时才需要数组)

(The array is only needed when you use minification in your build process)

要以编程方式调用特定的过​​滤器:

To call specific filter programmatically:

$filter('date')(new Date(), 'yyyy-MM-01');

$过滤器(名称)返回特定的过滤功能,您可以比你的参数来调用:

$filter(name)returns the specific filter function, which you can than call with your arguments:

var dateFilter = $filter('date');
var filteredDate = dateFilter(new Date(), 'yyyy-MM-01')

这篇关于注射dateFilter在AngularJs服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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