简体   繁体   English

AngularJS:如何在同一2个控制器的多个实例之间提供独立的通信?

[英]AngularJS: How to provide independent communication between multiple instances of the same 2 controllers?

I have two controllers allocated to two views: 我有两个控制器分配给两个视图:

[ResultsView ng-controller="ResultsCtrl"] [SearchView ng-controller="SearchCtrl"] [ResultsView ng-controller =“ ResultsCtrl”] [SearchView ng-controller =“ SearchCtrl”]

The Search View has many complex filters/options and is filled in by the user, then he/she can press "Search" on SearchView and Results should be populated into a Grid. 搜索视图具有许多复杂的过滤器/选项,并且由用户填写,然后他/她可以在SearchView上按“搜索”,然后应将结果填充到网格中。

Now I can send information between two either by a Service or by using $rootScope.$broadcast . 现在,我可以通过Service或使用$ rootScope。$ broadcast在两者之间发送信息。


Heres the problem I've run into: 这是我遇到的问题:

[ResultsView ng-controller="ResultsCtrl"][SearchView ng-controller="SearchCtrl"] [ResultsView ng-controller="ResultsCtrl"][SearchView ng-controller="SearchCtrl"] [ResultsView ng-controller="ResultsCtrl"][SearchView ng-controller="SearchCtrl"] [ResultsView ng-controller =“ ResultsCtrl”] [SearchView ng-controller =“ SearchCtrl”] [ResultsView ng-controller =“ ResultsCtrl”] [SearchView ng-controller =“ SearchCtrl”] [ResultsView ng-controller =“ ResultsCtrl”] [SearchView ng-controller =“ SearchCtrl”]

If I were to have multiple Result-Search sections on the same page, how can I ensure they each act independently from each other? 如果我要在同一页面上有多个“ 结果搜索”部分,如何确保它们各自独立发挥作用? Using the Service approach, the ResultsCtrl and SearchCtrl both have the defined service 使用Service方法,ResultsCtrl和SearchCtrl都具有定义的服务

.controller("searchCtrl", ["$scope", "$searchHttp", function ($scope, $searchHttp) {
.controller("resultsCtrl", ["$scope", "$searchHttp", function ($scope, $searchHttp) {

So I can't change how each instance of the controller behaves regarding the service. 因此,我无法更改控制器的每个实例在服务方面的行为。 Soon as one SearchCtrl calls the service, it will modify every ResultsCtrl instance. 一个SearchCtrl调用该服务后,它将立即修改每个ResultsCtrl实例。

Likewise using broadcasts $rootScope.$broadcast("searchResults"... will be picked up by every ResultsCtrl instance. 同样,使用广播$ rootScope。$ broadcast(“ searchResults” ...将由每个ResultsCtrl实例获取。

So whats the best way around this? 那么最好的解决方法是什么? I want to reuse the Results and Search View code since its basically the same. 我想重用Results和Search View代码,因为它们基本相同。 But I need to render each pair independently on the same page a few times. 但是我需要在同一页面上独立渲染每对数次。

I think the HTML structure you need is something like this. 我认为您需要的HTML结构是这样的。

<!--First-->
<div ng-controller="SearchCtrl">
    <div ng-controller="ResultsCtrl">
    </div>
</div>

<!--Second-->
<div ng-controller="SearchCtrl">
    <div ng-controller="ResultsCtrl">
    </div>
</div>

This HTML structure would help you to use independently the search results one's parent SearchCtrl created in ResultsCtrl. 这种HTML结构将帮助您独立使用在ResultsCtrl中创建的搜索结果的父SearchCtrl。

jsfiddle is here. jsfiddle在这里。 I hope this would help you. 希望对您有帮助。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM