首页 > IT职业 > IT图书 > 装饰 decorator,网上的资源比较少。记录一下
装饰decorator,这个东西,看了ng权威教程,里面介绍的东西很少。网上查了一下资料,大概有了自己的理解。
比如有两个module,A和B。module B依赖于module A,module B上有个service Mail,提供两个方法设定setReceiver(收件人) 和 setBody (发件人),在module B上应用Mail服务,如果想增加抄送人,就可以在Mail上装饰(扩展)一个方法addCC。
Moudle A
var Mail=function(){
this.receiver='';
this.body='';
this.cc=[];
}
Mail.prototype={
setReceiver:function(receiver){
this.receiver=receiver;
},
setBody:function(body){
this.body=body;
}
};
angular.module('A',[]).service('Mail',Mail); Module B
angular.module('B',['A']).config(function($provide){
$provide.decorator('Mail',function($delegate){
$delegate.addCC=function(cc){
this.cc.push(cc);
};
return $delegate;
})
})
.controller('test',function($scope){
Mail.addCC('csk');
console.log(Mail)
}); 来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/4346/viewspace-2798131/ ,如需转载,请注明出处,否则将追究法律责任。