AngularJS中控制器之间通信的正确方法是什么?

本文翻译自:What's the correct way to communicate between controllers in AngularJS?

What's the correct way to communicate between controllers? 控制器之间通信的正确方法是什么?

I'm currently using a horrible fudge involving window : 我目前正在使用涉及window的可怕软糖:

function StockSubgroupCtrl($scope, $http) {
    $scope.subgroups = [];
    $scope.handleSubgroupsLoaded = function(data, status) {
        $scope.subgroups = data;
    }
    $scope.fetch = function(prod_grp) {
        $http.get('/api/stock/groups/' + prod_grp + '/subgroups/').success($scope.handleSubgroupsLoaded);
    }
    window.fetchStockSubgroups = $scope.fetch;
}

function StockGroupCtrl($scope, $http) {
    ...
    $scope.select = function(prod_grp) {
        $scope.selectedGroup = prod_grp;
        window.fetchStockSubgroups(prod_grp);
    }
}

#1楼

参考:https://stackoom.com/question/lDMS/AngularJS中控制器之间通信的正确方法是什么


#2楼

Using $rootScope.$broadcast and $scope.$on for a PubSub communication. 使用$ rootScope。$ broadcast和$ scope。$ on进行PubSub通信。

Also, see this post: AngularJS – Communicating Between Controllers 另请参阅此文章: AngularJS - 在控制器之间进行通信


#3楼

GridLinked posted a PubSub solution which seems to be designed pretty well. GridLinked发布了一个PubSub解决方案,似乎设计得非常好。 The service can be found, here . 这里可以找到服务。

Also a diagram of their service: 还有他们的服务图:

消息服务


#4楼

Actually using emit and broadcast is inefficient because the event bubbles up and down the scope hierarchy which can easily degrade into performance bottlement for a complex application. 实际上使用发射和广播是低效的,因为事件在范围层次结构中上下波动,这很容易降级为复杂应用程序的性能瓶装。

I would suggest to use a service. 我建议使用服务。 Here is how I recently implemented it in one of my projects - https://gist.github.com/3384419 . 以下是我最近在我的一个项目中实现它的方法 - https://gist.github.com/3384419

Basic idea - register a pubsub/event bus as a service. 基本思路 - 将pubsub / event bus注册为服务。 Then inject that eventbus where ever you need to subscribe or publish events/topics. 然后在需要订阅或发布事件/主题的地方注入eventbus。


#5楼

Regarding the original code - it appears you want to share data between scopes. 关于原始代码 - 您似乎希望在范围之间共享数据。 To share either Data or State between $scope the docs suggest using a service: 要在$ scope范围内共享数据或状态,文档建议使用服务:

  • To run stateless or stateful code shared across controllers — Use angular services instead. 运行跨控制器共享的无状态或有状态代码 - 改为使用角度服务。
  • To instantiate or manage the life-cycle of other components (for example, to create service instances). 实例化或管理其他组件的生命周期(例如,创建服务实例)。

Ref: Angular Docs link here 参考:Angular Docs链接在这里


#6楼

Here's the quick and dirty way. 这是快速而肮脏的方式。

// Add $injector as a parameter for your controller

function myAngularController($scope,$injector){

    $scope.sendorders = function(){

       // now you can use $injector to get the 
       // handle of $rootScope and broadcast to all

       $injector.get('$rootScope').$broadcast('sinkallships');

    };

}

Here is an example function to add within any of the sibling controllers: 这是一个在任何兄弟控制器中添加的示例函数:

$scope.$on('sinkallships', function() {

    alert('Sink that ship!');                       

});

and of course here's your HTML: 当然这是你的HTML:

<button ngclick="sendorders()">Sink Enemy Ships</button>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值