Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 发布/订阅设计模式angularjs服务_Javascript_Angularjs_Design Patterns_Publish Subscribe - Fatal编程技术网

Javascript 发布/订阅设计模式angularjs服务

Javascript 发布/订阅设计模式angularjs服务,javascript,angularjs,design-patterns,publish-subscribe,Javascript,Angularjs,Design Patterns,Publish Subscribe,我一直在尝试使用马克·拉科克发布的答案 我很难理解他的答案。特别是本部分: angular.forEach(event1ServiceHandlers, function(handler) { handler(some_data); }); event1ServiceHandlers数组是否填充了在此forEach循环中触发的函数(此处称为handler) 我认为用一个好的例子来理解发布/订阅是如何设置的会容易得多 我有两个需要沟通的服务,但我想避免$rootScop

我一直在尝试使用马克·拉科克发布的答案

我很难理解他的答案。特别是本部分:

angular.forEach(event1ServiceHandlers, function(handler) {
            handler(some_data);
});
event1ServiceHandlers数组是否填充了在此forEach循环中触发的函数(此处称为handler)

我认为用一个好的例子来理解发布/订阅是如何设置的会容易得多

我有两个需要沟通的服务,但我想避免$rootScope。$broadcast,所以从我所读到的内容来看,发布/订阅服务是最好的方法。我的一个服务需要在另一个服务上执行一个函数,但该服务已经将我的第一个服务作为依赖项,因此由于循环依赖性,我无法同时执行这两种方式


我的问题:假设您有两个angularjs服务(工厂),如果服务2已经将服务1作为依赖项,那么服务1如何在服务2上执行函数。不使用$broadcast和$on在他的示例中,
NotificationService
是任何现有服务都将依赖的新服务。他提供了一个
Service1
的实现,但是
Service2
本质上是一样的……两者都依赖于
NotificationService
,彼此都不了解

Service1
Service2
通过调用
NotificationService.onEvent1(event1ocated)订阅事件并通过调用NotificationService.Event1Occessed(my_数据)触发事件

event1ServiceHandlers数组是否填充了在此forEach循环中触发的函数(此处称为handler)

如果服务2已经将服务1作为依赖项,那么服务1如何在服务2上执行函数

像以前一样创建服务3,
通知服务

.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);
        }
    };
}])
让服务2向通知服务注册回调函数

.factory('Service2', ['NotificationService',
function(NotificationService) {
    // event1 handler
    var doSomething = function(someData) {
        console.log('S2', someData);
        // do something here
    }
    // subscribe to event1
    NotificationService.onEvent1(doSomething);
    return {
      // define public API for Service2 here
    }
}])
每当服务1希望在服务2上执行函数
doSomething()
时,它可以发布
event1occurrent
事件:

.factory('Service1', ['NotificationService',
function(NotificationService) {
    var someData = ...;
    return {
       // define public API for Service1 here
       callService2Method: function() {
         // publish event
         NotificationService.event1Happened(someData);
       }
    }
}])

这一部分我理解,但我想用一个实际的例子来说明它在哪里被使用,订阅是如何在使用NotificationServices的服务之间建立的。事实上,没有太多了……假设Service1只负责触发一个事件,Service2只负责处理一个事件。然后,Service2将通过调用
NotificationService.onEvent1(event1occurrent)通过通知服务进行订阅
和Service1将通过调用NotificationService.Event1Occessed(my_数据)发布事件谢谢!(我删除了您希望删除的评论)。从未想过可以将函数存储在数组中,然后循环执行它们。使用第三种服务订阅也花了我一点时间:)@AshkanAldini,是的,设计模式有点难以理解。我稍微修改了示例:我重命名了service2函数
doSomething()
。希望这能让未来的读者更容易理解。