如何而不是它​​们之间使用SharedService控制器之间的通信? [英] How to communicate between controllers while not using SharedService between them?

查看:109
本文介绍了如何而不是它​​们之间使用SharedService控制器之间的通信?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在阅​​读所有关于控制器和指令之间的沟通问题的答案,但似乎发生了我使用共享服务,并将它注入到他们每个人。当我开发一个非常大规模的应用程序,我不知道我的网页将不得不在里面。我可能有2个控制器需要在它们之间进行沟通,我也可以有5个指令和2个控制器在同一页面。我不从零开始什么事情在里面我的看法/页知道,所以我需要一个更好的方法是如何在它们之间进行通信。我在寻找更好的方式来做到这一点,正确的方式AngularJS

I was reading all the answers about communication between controllers and directive, but is seems to me occurred using shared service and inject it to each one of them. When I'm developing a very large scale application, I don't know what my page is going to have in it. I may have 2 controllers need to communicate between them, and I also may have 5 directive and 2 controllers in the same page. I don't know from scratch what is going to be inside my view/page, so I need a better way how to communicate between them. I'm looking for that better way to do it, the right AngularJS way.

http://i60.tinypic.com/2z87q05.png

以上是我的工作我的看法例如:在左边我有一个指令树,右边我有图表控制器和指令网格。我可能比这多,但是这是什么,我可能有一个很好的例子。请记住,视图可以包含X其它的组件,你不从一开始将是什么在它知道了。

Above is my view example I'm working on: on the left side I have a directive tree, on the right side I have chart controller and directive grid. I may have more than this, but this is a good example of what I may have. Keep in mind, the view can have X componenets, you don't know from the beginning what will be in it.

现在,让我们说每一个我选择左侧的树节点的时候,我希望能够告诉其他控制器,nodeSelectedChange事件happend。我不希望注入他们每个人持有该信息服务,所以我想的东西就像有一个页面管理器,这是所有视图的父亲。我所有的控制器/我的页面中的指令只能由PageManagerController与对方通话,他是在知道他有它的内页的唯一的事情。

Now, lets say each time I select node in tree on the left, I want to be able to tell the other controllers that nodeSelectedChange event happend. I don't want to inject each one of them a service that holds that info, so I thought about something like having a Page Manager Controller, which is the father of the all view. All my controllers/directives inside my page should talk with each other only by the PageManagerController, he is the only thing in page that knows what he has inside of it.

重要这里要记住:树不知道页面有图表或网格,他们并不需要知道对方是为了交流。页面经理都知道的一切,现在我想它使魔 - 它应该有一个服务?如若对方组件具有服务和服务可以使用PageManager服务说话?

Important here to keep in mind: The tree don't know the page has chart or grid, they don't need to know each other in order to communicate. The page manager knows everything, now I want it to make the magic - Should it has a service? Should each other component has service and service can talk with PageManager service?

帮我想。希望我可以连接所有的点,以创建通信更好的方法。
我觉得sharedService方式非常适用于小型应用程序,当你知道从什么开始在应用中事,但大多数时候 - 你只是不知道是谁,并且要使用你的指令时,应能和大家一起交流。

Help me to think. Hope I can connect all the dots to create a BETTER way for communication. I think the sharedService way is good for small app, when you know from start what is going on in your app, but most of the time - You just don't know who and when is going to use your directive, it should be able to talk with everyone.

我不喜欢的活动:


  1. 的事件被解雇的控制器内,而应该是一个服务里面。

  2. 监听控制器应该知道他会听太事件的名称。如果我改变事件名称为$发射或$播出,我需要在所有的应用程序去各地听众的$(eventName的),并变更为唯一的名称。

  3. 指令就像是一个黑盒子,我不想来检查它每次的时间内,找到他正在广播的事件的名称,以便与它交流。

我需要一种方法来暴露事件名字出指令,可能与连接到控制器的服务。

I need a way to exposed the events NAMES out of the directive, probably with a service connected to that controller.

推荐答案

这真的取决于你想从树上指令到页面的其他部分共享的信息类型。

It really depends on the type of information you want to share from the tree directive to the other sections of the page.

您有在你面前的几个选项:

You have a few options in front of you:

正如有人上面提到的,你可以触发一个事件也能聆听到各种控制器和/或服务的活动。这就是说,它可以得到真难看,真快。跟踪事件,并找出哪些事件侦听器是活跃在给定的点可以给最优秀的工程师偏头痛!

As someone mentioned above, you could fire an event and listen for events in various controllers and/or services. That said, it can get really ugly, really fast. Tracing events and figuring out what event listeners are active at a given point can give the best engineers a migraine!

另一种选择是使用一个服务,比方说,一个PageManagerService。在这种情况下,

Another option would be to use a Service, let's say a PageManagerService. In that case,


  • 树项目的每一次点击将设定在PageManagerService一些信息,说哪个页面,哪些对象,而且它需要显示
  • 哪些项目
  • 需要更改每个组件可以

    • 注册一个监听器被触发时,PageManagerService变化

    • 添加一块手表上的服务和运行其code当PageManagerService变化

    该服务本身只是要共享的状态,这将是对高达就是了如何消费,并在状态变化作出反应的指示和组件。

    The service itself is just going to be shared state, and it would be upto the directives and components on how it wants to consume and respond to changes in the state.

    不过我觉得这个的越多,这似乎是从类似的 UI路由器 。 UI路由器允许您定义的状态,并以不同的方式状态变化的响应页面的不同部分。每个部分都可以通过加载到自己的方式的状态变化做出反应,

    But the more I think about this, the more it seems like a good use case from something like UI Router. UI Router allows you to define states, and have different parts of the page respond in different ways to state changes. Each section could respond to a state change in its own way, by loading


    • 不同的控制器

    • 不同的模板,可能与不同的组件和部件

    所以,你最终会是有一个结构

    So what you would end up having is a structure with


    • 在左侧的树指令

    • 命名,假设一个用户界面视图,顶

    • 命名的UI视图,让我们说,底部

    在你的树指令,每个项目就可以成为一个 UI-SREF ,这是说,而不是重定向到URL的只是一种奇特的方式,重定向到一个状态。

    Each item in your tree directive can then be a ui-sref, which is just a fancy way of saying instead of redirecting to a URL, redirect to a state.

    然后,您可以定义自己的配置在你的应用程序在一个地方,像这样:

    You could then define your configurations in your application in a single place, like so:

    $stateProvider.state('dashboard', {
      views: {
        "top": { templateUrl: 'my/dashboard.html', controller: 'DashboardCtrl'}
        "bottom": { templateUrl: 'my/dashboard-grid.html', controller: 'DashboardGridCtrl'}
      }
    })
    

    同样,你可以为树中的指令,每一个项目的状态定义,每个人只是到不同状态的链接。

    Similarly, you could have a state definition for each item in your tree directive, and each one just be a link to a different state.

    当然,国家的定义在配置部分做在AngularJS应用程序,你会知道的是应用程序启动之前。如果你需要动态的状态呢?

    Of course, the state definitions are done in your config section in an AngularJS application, which you would know is before the application starts. What if you needed dynamic states as well?

    好了,几个答案/想法,导致你为的方式,以及:

    Well, a few answers / thoughts to lead you down the way for that as well:


    • 的控制器和服务必须是predefined,你不会是动态创建HTML内容和/或JS控制器

    • 这是可能的暴露$ stateProvider作为配置部分的全局变量和动态调用stateProvider.state控制器/服务,只要您有新的状态定义中。

    • 更往往不是虽然,控制器和HTML保持不变,而我们只需要使用不同的参数来触发不同的状态。这可以很容易地通过调用来完成的 transitionTo 功能过渡到各种状态参数定义的状态。

    • The controllers and services would have to be predefined, you would not be dynamically creating HTML content and/or JS controllers
    • It is possible to expose the $stateProvider as a global variable in the config section, and dynamically call stateProvider.state inside a controller / service, wherever you have new state definitions.
    • More often than not though, the controllers and HTML remain constant, and we just need to trigger the different states with various parameters. That can easily be done by calling transitionTo function to transition to a defined state with various state parameters.

    这篇关于如何而不是它​​们之间使用SharedService控制器之间的通信?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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