#A: $decorator - extend method on service
by Krzysztof Safjanowski
HTML
<div ng-app="app">
<div ng-controller="ctrl">
{{ mail }}
</div>
</div>
JavaScript
angular.module('app', [])
.controller('ctrl', function($scope, emailService) {
emailService.setContent('enjoy')
$scope.mail = emailService.send('foo');
})
.service('emailService', function() {
this.content = '';
this.setContent = function(content) {
this.content = content;
};
this.send = function(recipient) {
return 'mail "'+ this.content +'" sending to ' + recipient;
};
}).
config(function($provide) {
$provide.decorator('emailService', function($delegate) {
var delegatedSend = $delegate.send;
$delegate.send = function(recipient) {
return delegatedSend.bind(this)(recipient) + ' on ' + new Date()
}
return $delegate;
})
})