Boris D. Teoharov
Boris D. Teoharov

Reputation: 2388

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?

Upvotes: 1

Views: 689

Answers (2)

Boris D. Teoharov
Boris D. Teoharov

Reputation: 2388

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.

Upvotes: 1

charlietfl
charlietfl

Reputation: 171669

You can update a provider object in a run() block where you can inject services

Upvotes: 1

Related Questions