多重控制器和一个指令之间的沟通 [英] Communicating between a Multiple Controllers and a directive

查看:114
本文介绍了多重控制器和一个指令之间的沟通的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个指令,一个权力HTML5画布的可视化。这个指令具有广泛的阵列的方法来修改可视化的不同部分。问题是,有不同的父/子/兄弟关系的多个控制器需要传达给这个指令。现在我把它发出连接事件到指令的父控制器,然后将其广播的指令的这个pretty可怕的方式。

I have a directive that powers an HTML5 Canvas visualization. This directive has a wide array of methods to modify different parts of the visualization. The issue is that multiple controllers that have a different parent/child/sibling relationship need to communicate to this directive. Right now I have it wired this pretty awful way of emitting events up to the parent controller of the directive and then broadcasting them to the directive.

我听说用服务做这样的事情的,但没有真正解释为什么。我想用这样的事情的:

I have heard of using a service to do something like this, but nothing really explain why. I thought of using something like this:

angular.service('CanvasCommunication', function($rootScope) { 
   this.canvasAction = function() { $rootScope.broadcast('canvasAction'); };
}

然后在画布上监听到实际执行该操作。该服务可以再注入与画布通信的任何控制器。

And then have listener in the canvas to actual execute that action. This service could be then injected into any controller that communicates with the canvas.

这样做的问题是,$ rootScope.broadcast()有糟糕的表现,我想,以确保该通信信道是建立在最有效的方式。

The issue with this is that $rootScope.broadcast() has terrible performance and I want to make sure this communication channel is built in the most efficient way.

有没有人处理这样的事情和思想更好的东西?

Has anybody dealt with something like this and thought of something better?

推荐答案

我有同样的问题 - 需要的控制器与应用程序发送消息给对方,等在我的项目的交相辉映,不同的部分进行交互,我已经实现了一个MessageService。这里有一个(但说实话绰绰有余)的一个非常基本的版本:

I've had the same issue - controllers needing to interact with each other, different parts of the app sending messages to each other, etc. In my projects, I've implemented a MessageService. Here's a very basic version of one (but honestly more than sufficient):

module.factory('MessageService',
  function() {
    var MessageService = {};

    var listeners = {};
    var count = 0;
    MessageService.registerListener = function(listener) {
      listeners[count] = listener;
      count++;

      return (function(currentCount) {
        return function() {
          delete listeners[currentCount];
        }
      })(count);
    }

    MessageService.broadcastMessage = function(message) {
      var keys = Object.keys(listeners);

      for (var i = 0; i < keys.length; i++) {
        listeners[keys[i]](message);
      }
    }

    return MessageService;
  }
);

您可能希望有特定对象注册的侦听器,并通过主题过滤消息,还是不行。矿还排队在邮件主题,直到他们被清除,以使这些信息可以被看作当一个新的视图负载(以一对恩。成功 - 已保存的文件与页面变化)。

You might want to have listeners registered for particular subjects, and filter messages by subject, or not. Mine also queue messages on subjects until they're cleared, so that the messages can be viewed when a new view loads (in order to pair for ex. 'Success - Saved file' with a page change).

这篇关于多重控制器和一个指令之间的沟通的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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