services.js
.service('emailService', function() {
this.email = "give me!";
this.setContent = function(content) {
this.email = content;
};
this.send = function(recipient) {
return 'sending "' + this.email + '" to ' + recipient;
};
})
app.js
.config(function($provide){
$provide.decorator('emailService',function($delegate){
$delegate.sendWithSignature=function(recipient,email){
return 'sending "'+this.email+'" to '+recipient+" by "+email
}
return $delegate
})
})
controller.js
$scope.resole = emailService.sendWithSignature("lin", "how are you !");
alert($scope.resole);
博客展示了AngularJS中服务装饰器的使用。在services.js里定义了emailService服务,app.js中使用$provide.decorator对其进行扩展,添加了sendWithSignature方法,最后在controller.js中调用该扩展方法并弹出结果。
1345

被折叠的 条评论
为什么被折叠?



