How to communicate between directive (tree component) and any other controller or directive in AngularJS app?

巧了我就是萌 提交于 2019-12-11 04:50:03

问题


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.

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.

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


回答1:


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

Hope that helps



来源:https://stackoverflow.com/questions/25349845/how-to-communicate-between-directive-tree-component-and-any-other-controller-o

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!