在AngularJS模块之间的通信 [英] Communication between modules in AngularJS

查看:462
本文介绍了在AngularJS模块之间的通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想这是可能有一个shellpage内连接到不同地区的许多角模块。
但模块AngularJS交谈对方?
如果是的话,怎么样?

I guess it is possible to have many angular-modules attached to different regions within one shellpage. But can modules in AngularJS "talk" to each other? If yes, how?

推荐答案

有多种方法模块可以互动或分享信息

There are various ways module can interact or share information


  1. 一个模块可以注入另一模块,在这种情况下,容器模块有权访问喷射模块中的所有元素。如果你看一下角种子项目,是为指令,控制器,过滤器创建的模块等等,像这样

  1. A module can be injected into another module, in which case the container module has access to all elements of the injected module. If you look at angular seed project, modules are created for directive, controllers, filters etc, something like this

angular.module(对myApp,[myApp.filters,myApp.services,myApp.directives,myApp.controllers])
这更是一个重新的可用性机制,而不是沟通机制。

angular.module("myApp", ["myApp.filters", "myApp.services", "myApp.directives", "myApp.controllers"]) This is more of a re usability mechanism rather than communication mechanism.

第二个选择是由@Eduard解释将是使用服务。因为服务是独立的,并可以被注射到任何控制器,它们可以作为一个通信机制

The second option is as explained by @Eduard would be to use services. Since services are singleton and can be injected into any controller, they can act as a communication mechanism.

由于@Eduard再次指出了第三种选择是使用$范围的对象,因为它是提供给所有子控制器使用父控制器。

As @Eduard again pointed out the third option is to use parent controller using $scope object as it is available to all child controllers.

您也可以注入$ rootScope到需要进行互动,并使用$广播和$上的方法来创造一个控制器使用酒馆\\子机制交互服务总线模式控制器。

You can also inject $rootScope into controllers that need to interact and use the $broadcast and $on methods to create a service bus pattern where controllers interact using pub\sub mechanism.

我会偏向第四选项。看到一些更多的细节在这里太<一个href=\"http://stackoverflow.com/questions/11252780/whats-the-correct-way-to-communicate-between-controllers-in-angularjs\">What's正确的方式在AngularJS控制器之间的通信?

I would lean towards 4th option. See some more details here too What's the correct way to communicate between controllers in AngularJS?

这篇关于在AngularJS模块之间的通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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