AngularJS Foundations

Decorator Demo Code Javascript

var upstream = angular.module('thirdParty', []);
upstream.service('emailService', function() {
  this.email = "";

  this.setContent = function(content) {
    this.email = content;
  };

  this.send = function(recipient) {
    return 'sending "' + this.email + '" to ' + recipient;
  };
});

var app = angular.module('myApp', ['thirdParty']);

app.config(function($provide) {
  $provide.decorator('emailService', function($delegate) {

    $delegate.sendWithSignature = function(recipient, signature) {
      return 'sending "' + this.email + '" to ' + recipient + " by " + signature;
    };
    return $delegate;
  });
});

app.controller('MainCtrl', function($scope, emailService) {
  emailService.setContent("Greeting!!");
  $scope.emailComplete = emailService.sendWithSignature('[email protected]', 'tamakisquare');
});