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

查看:21
本文介绍了如何在指令(树组件)和 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/angular-ui-tree/

Here is my scenario: Left side of my page I have a side bar with a simple folder/files tree. I have used angular Tree Component and it works great for me: http://jimliu.github.io/angular-ui-tree/

在页面右侧,我有一个正在被用户导航更改的视图.每次用户选择树上的任何节点时,我都希望能够告诉任何右侧页面控制器已选择哪个节点.树上的每个选择都应通知任何想要听取该更改的人.

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?

推荐答案

为了说明如何实现,我创建了一些示例代码,使用服务在控制器和指令之间共享数据:

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=preview

希望有帮助

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

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