javascript - 以 Angular 扩展现有服务

标签 javascript angularjs

我正在使用 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/

相关文章:

javascript - 在 AngularJS 中的多个作用域变量上设置 $watch

javascript - 在 Ionic 中安全地存储 API key

javascript - 如何让列表按姓氏字母顺序排序,但在javascript中将名字放在前面

javascript - 从名字中获取数字

javascript - Dropzone 始终返回 'empty($_FILES)' 作为 true

javascript - Angular JS ng-if 在链接上

javascript - $http({method}) 不工作,但 $http.get 工作

javascript - 没有项目的中继器中的 Angular 加载 img src

javascript - 如何在我的 angularjs 应用程序中使用 jsfiddle 代码

javascript - IE 10 : cannot get elements with name composed with only digit