我正在使用 Angular 咆哮服务( angular-growl ),我需要一种方法来添加特定类型的咆哮,例如
.controller('Controller', ['$scope', 'growl', function ($scope, growl) {
growl.add("Message", "INFO");
}
当 Angular 启动过程时,是否可以将新方法附加到咆哮服务(或将方法附加到任何服务)?
最佳答案
基本上,您可以使用 Angular decorator
的概念来扩展现有服务。这是通过 $provide.decorator 完成的。一个实现看起来像这样:
app.config(function($provide) {
$provide.decorator('growl', function($delegate) {
$delegate.add = function(message, type) {
// implementation for add.
};
return $delegate;
});
});
在这篇 SO 帖子 What are "decorators" and how are they used? 中了解更多相关信息
关于javascript - 以 Angular 扩展现有服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28317340/