0

我想弹出一条 toastr 消息以显示 $log 消息。IE:

$log.info('test'));

似乎在 AngularJS 1.2.19 及更高版本中,$provide.decorator扩展 $log 功能的好方法。当然,我使用的是 1.2.18。有没有办法在 1.2.18 中做到这一点?

理想情况下,我想扩展现有功能,而不是完全覆盖它。

我不想修改 Angular 源代码。

4

1 回答 1

4

装饰器机制是创建提供者的简写,该提供者获取先前的定义并返回它的新版本。

因此,您可以按如下方式模仿此功能:

module.provider('$log', function ($logProvider) {
    // $logProvider here is the one previously defined, from 'ng'
    // unless someone else is overriding too.
    this.$get = function ($injector) {
        // Invoke the original provider.
        var $log = $injector.invoke($logProvider.$get);
        var oldInfo = $log.info;
        // Override the method.
        $log.info = function info() {
            oldInfo.apply(this, arguments);
            // (not actually useful to alert arguments; just an example)
            alert(arguments);
        }
        return $log;
    }
});

当然,这会修改所有依赖项的对象,即使他们对标准$log接口非常满意,也意味着您无法直接在应用程序中查看哪些服务依赖于标准接口,哪些服务依赖于您的增强接口。如果您只想通过包装来更改现有方法的行为(例如,将日志发送到您的服务器,或者如您在问题中提到的那样显示烤面包机),这可能很有用,但可能是添加其他方法的冒险选择现有调用者不期望的方法,您可能会意外破坏与标准接口的兼容性。

相反,最好提供一项新服务,您可以在需要扩展的地方使用该服务,并将内置$log接口保留为标准。这样就更容易区分并避免标准调用者的意外行为变化。这看起来与上面类似,但在细节上略有不同:

module.provider('anotherLog', function ($logProvider) {
    this.$get = function ($injector) {
        var $log = $injector.invoke($logProvider.$get);

        // Create a new object rather than extending the existing one in-place.
        // This way the new method is only visible to callers that request
        // 'anotherLog' instead of just '$log'.
        var anotherLog = angular.extend(Object.create($log), {
            sayHello: function sayHello() {
                console.log('Hello!');
            }
        })

        return anotherLog;
    }
});

这两种方法都利用了这样一个事实,即在应用程序初始化期间,使用单独的“提供程序注入器”来处理提供程序之间的依赖关系。$injector这与创建应用程序后使用的 main 不同。提供程序注入器包含到目前为止已定义的所有提供程序,但不包含生成的服务,例如$log. 提供者函数本身是通过提供者注入器注入的,而它的$get方法(如这些示例所示)是通过 main 注入的$injector

由于这种区别,如果您依赖任何其他服务来显示您的烤面包机,则必须依赖于它们的$get方法本身,而不是作为一个整体的提供者。这将允许您访问 main 中的服务$injector,而不仅仅是来自提供程序注入器的提供程序。

于 2014-12-26T19:42:55.580 回答