简体   繁体   中英

use $filter service inside module.config

As per the AngularJS documentation it seems that it is only possible inject providers in module.config . However, I have to configure some 3rd party service using its provider ServiceXProvider and set it up like this:

ServiceXProvider.format = function format(x) { return $filter('date')(x, "yyyy-MM-dd"); }

$filter is obviously a service and not a provider and I can not inject it into the module.config .

Is there any reasonable workaround for this scenario?

您可以在run()块中更新提供程序对象,在其中可以注入服务

As @charlietfl suggested it was possible in this case to inject ServiceX instance in module.run() (not the provider but the instance).

So

module.config(function (ServiceXProvider, $filter) {
  ServiceXProvider.format = function format(x) { return $filter('date')(x, "yyyy-MM-dd"); }
});

became

module.run(function (ServiceX, $filter) {
  ServiceX.format = function format(x) { return $filter('date')(x, "yyyy-MM-dd"); }
});

It is not perfect but as far as I am concerned it works. Notice ServiceXProvider became ServiceX later.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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