AngularJS Decorators


  • decorator(name, decorator);


Decorator is function that allow a service, factory, directive or filter to be modified prior to its usage. Decorator is used to override or modify the behavior of the service. The return value of the decorator function may be the original service, or a new service that replaces, or wraps and delegates to, the original service.

Any decorating must be done in angular application's config phase by injecting $provide and using it's $provide.decorator function.

The decorator function has a $delegate object injected to provide access to the service that matches the selector in the decorator. This $delegate will be the service you are decorating. The return value of the function provided to the decorator will take place of the service, directive, or filter being decorated.

One should consider using decorator only if any other approach is not appropriate or proves to be too tedious. If large application is using same service, and one part is changing service behavior, it's easy to create confusion and/or bugs in the process.

Typical use case would be when you have a 3rd party dependency which you can't upgrade but need it to work little differently or extend it.