扩展现有服务的角度 [英] Extend existing service in angular

查看:62
本文介绍了扩展现有服务的角度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用角度咆哮服务(角度咆哮),我需要一种方法添加特定类型的咆哮声,例如

I'm using angular growl service (angular-growl), and i need one method to add growl with specific type, e.g.

.controller('Controller', ['$scope', 'growl', function ($scope, growl) {
     growl.add("Message", "INFO");
}

在角度启动过程中是否可以将新方法附加到咆哮服务(或将方法附加到任何服务)?

is it possible to append new method to growl service (or append method to any service) when angular start process?

推荐答案

基本上,您可以使用Angular decorator 的概念来扩展现有服务.这是通过 $ provide.decorator 完成的.一个实现看起来像这样:

Basically you can use the concept of Angular decorator to extend the existing service. This is done through $provide.decorator. An implementation would look something like this:

app.config(function($provide) {
  $provide.decorator('growl', function($delegate) {
    $delegate.add = function(message, type) {
      // implementation for add.
    };
    return $delegate;
  });
});

在此SO帖子中了解有关此内容的更多信息什么是";装饰器"以及如何使用?

Read more about it in this SO post What are "decorators" and how are they used?

这篇关于扩展现有服务的角度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆