angular JS - 在非依赖服务之间进行通信 [英] angular JS - communicate between non-dependend services

查看:19
本文介绍了angular JS - 在非依赖服务之间进行通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 angular 新手,遇到了一个 catch-22:

I am new in angular and encounter a catch-22:

事实:

  1. 我有一个记录我的东西的服务(my-logger).

  1. I have a service that logs my stuff (my-logger).

我已经用我自己的实现替换了 $ExceptionHandler(angular),它将未捕获的异常转发到我的记录器服务

I have replaced the $ExceptionHandler (of angular), with my own implementation which forwards uncaught exceptions to my-logger service

我有另一个服务 pusher-service,每当使用my-logger"在我的应用程序中的某处记录致命消息时,都需要通知该服务.

I have another service, pusher-service, that needs to be notified whenever a fatal message is to be logged somewhere in my application using 'my-logger'.

问题:

我不能让 'my-logger' 依赖于 'pusher',因为它会产生循环依赖(因为 'pusher' 使用 $http.圆圈:$ExceptionHandler -> my-logger -> pusher -> $http -> $ExceptionHandler...)

I can't have 'my-logger' be depend on 'pusher' since it will create circular dependency (as 'pusher' uses $http. The circle: $ExceptionHandler -> my-logger -> pusher -> $http -> $ExceptionHandler...)

我的尝试:

为了使这两个服务相互通信,我想在 pusher-service 上使用 $watch:监视 $rootscope 上的一个属性,该属性将在 my-logger 中更新.但是,当尝试在my-logger"中使用 $rootScope 时,为了更新pusher"监视"的属性,我在循环依赖上失败了,因为事实证明 $rootscope 依赖于 $ExceptionHandler(圆圈: $ExceptionHandler -> my-logger -> $rootScope -> $ExceptionHandler).

In order to make these 2 services communicate with each other, I wanted to use $watch on the pusher-service: watches a property on $rootscope that will be updated in my-logger. But, when trying to consume $rootScope in 'my-logger', in order to update the property on which the 'pusher' "watches", I fail on circular dependency as it turns out that $rootscope depends on $ExceptionHandler (the circle: $ExceptionHandler -> my-logger -> $rootScope -> $ExceptionHandler).

试图找到一个选项来在运行时获取在其上下文my-logger"服务中工作的范围对象.找不到这样的选项.

Tried to find an option to get, at runtime, the scope object that in its context 'my-logger' service works. can't find such an option.

也不能使用广播,因为它需要 my-logger 访问范围 ($rootScope),而这是不可能的,如上所示.

Can't use broadcast as well, as it requires my-logger to get access to the scope ($rootScope) and that is impossible as seen above.

我的问题:

是否有一种角度方式让两个服务通过第 3 方实体进行通信?

Is there an angular way to have two services communicate through a 3rd party entity ?

知道如何解决这个问题吗?

Any idea how this can be solved ?

推荐答案

使用第三个服务作为通知/发布订阅服务:

Use a 3rd service that acts as a notification/pubsub service:

.factory('NotificationService', [function() {
    var event1ServiceHandlers = [];
    return {
        // publish
        event1Happened: function(some_data) {
            angular.forEach(event1ServiceHandlers, function(handler) {
                handler(some_data);
            });
        },
        // subscribe
        onEvent1: function(handler) {
            event1ServiceHandlers.push(handler);
        }
    };
}])

以上,我只展示了一种事件/消息类型.每个额外的事件/消息都需要自己的数组、发布方法和订阅方法.

Above, I only show one event/message type. Each additional event/message would need its own array, publish method, and subscribe method.

.factory('Service1', ['NotificationService',
function(NotificationService) {
    // event1 handler
    var event1Happened = function(some_data) {
        console.log('S1', some_data);
        // do something here
    }
    // subscribe to event1
    NotificationService.onEvent1(event1Happened);
    return {
        someMethod: function() {
           ...
           // publish event1
           NotificationService.event1Happened(my_data);
        },
    };
}])

Service2 的编码类似于 Service1.

Service2 would be coded similarly to Service1.

请注意 $rootScope、$broadcast 和作用域如何不用于此方法,因为服务间通信不需要它们.

Notice how $rootScope, $broadcast, and scopes are not used with this approach, because they are not needed with inter-service communication.

通过上述实现,服务(一旦创建)在应用程序的生命周期内保持订阅状态.您可以添加处理取消订阅的方法.

With the above implementation, services (once created) stay subscribed for the life of the app. You could add methods to handle unsubscribing.

在我当前的项目中,我使用相同的 NotificationService 来处理控制器范围的发布订阅.(参见 更新 Angularjs 中的时间前"值和Momentjs 如果有兴趣).

In my current project, I use the same NotificationService to also handle pubsub for controller scopes. (See Updating "time ago" values in Angularjs and Momentjs if interested).

这篇关于angular JS - 在非依赖服务之间进行通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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