Decorating a Service in Angular

An Angular technique I've found useful recently is decorating an existing service. Say, for example, you important a 3rd-party library that creates an Angular factory() for generating widgets. These widgets can do a lot of useful things, but you'd really like them to be able to sing(). Rather than attaching a sing() method to each widget when you create it, you can use a decorator to attach the sing() method to the prototype of the object:

app.config(function ($provide) {  
  $provide.decorator('Widget', function ($delegate) {
    $delegate.prototype.sing = function () {
      console.log('LA LA LA LA LA')

    return $delegate

Now anytime you create a new widget with new Widget(), the resulting object will have the sing() method.

Fancy, huh?