如何指令(树组件)和AngularJS应用任何其他控制器或指令之间沟通? [英] How to communicate between directive (tree component) and any other controller or directive in AngularJS app?

查看:175
本文介绍了如何指令(树组件)和AngularJS应用任何其他控制器或指令之间沟通?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我读了这么多的信息,你对控制器之间的通信的所有份额,那我现在很困惑做到这一点的最好办法。我明白我应该使用的服务,但真的不知道它从大家写的例子的意思。

First of all, I have read so much info you all share about communication between controllers, that I'm now so confused about the BEST way to do it. I understand I should use service, but don't really know what it means from the examples you all wrote.

下面是我的情况:
我的页面左侧我有一个简单的文件夹/文件树中的一个酒吧。我用角树组件,它为我的伟大工程: http://jimliu.github.io /角度的UI树/

在页面的右侧,我有一个正在被用户导航改变了看法。
每次用户选择树中的任何节点,我希望能够告诉已经选择了哪个节点的任何右侧页面控制器。树每个选择应该通知谁想要听这种变化的任何一个。

On the right side of the page, I have a view that is being changed by user navigation. Each time user select any node on the tree, I want to be able to tell any right side page controller which node has been selected. Each selection on tree should notify any one who want to listen to that change.

我不喜欢使用$ rootScope和火灾事件从服务的理念,这是唯一的方法是什么?

I don't like the idea of using $rootScope and fire event from the service, Is that the only way?

推荐答案

要说明它是如何做的,我使用的是服务共享控制器和指令之间的数据创造了一些例如code:

To illustrate how it can be done, I created some example code using a service to share data between Controllers and Directives:

app.factory('SharedService', function() {
  return {
    sharedObject: {
      value: ''
    }
  };
});

http://plnkr.co/edit/Q1VdKJP2tpvqqJL1LF6m?p=$p$ PVIEW

希望帮助

这篇关于如何指令(树组件)和AngularJS应用任何其他控制器或指令之间沟通?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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