angularjs – 角色,广播不工作

我的目的是将一些数据从角度控制器发送到另一个.

控制器必须发送数据:

myApp.controller('MapCtrl',['$scope','$http',function ($scope,$http) {
    $scope.loadData = function () {
        $http.get('/map/GetListDB').success(function (data,status,headers,config) {

            //Logic here is working fine,it creates a table named "ExcelCols" which is a table of strings

            $scope.$broadcast("SET_EXCEL_TITLES",$scope.ExcelCols);
        })
    }

}]);

这是第二个控制器

myApp.controller('ExcelViewCtrl',function($scope,$http) {
    $scope.$on("SET_EXCEL_TITLES",function (event,excelCols) {

        //this event is never fired

        $scope.ExcelCols = excelCols;
    });
}]);

我的观点是这样设计的:

<body ng-app="myApp">
    <div ng-controller="MapCtrl">
         //everything OK here
    </div>

    <div ng-controller="ExcelViewCtrl">
      <table>
        <thead>
            <tr>
                <th ng-repeat="col in ExcelCols">{{col}}</th>
            </tr>
        </thead>
       </table>          
    </div>

 </body>
根据控制器的结构,w.r.t到$broadcast消息将被路由.

根据documentation

dispatches an event name downwards to all child scopes (and their
children) notifying the registered ng.$rootScope.Scope#$on listeners.

这意味着发送广播的控制器应该在子控制器html的父级html上定义.

根据您的html结构,使用$rootScope.$broadcast.将$rootScope注入MapCtrl并调用$broadcast方法.

相关文章

ANGULAR.JS:NG-SELECTANDNG-OPTIONSPS:其实看英文文档比看中...
AngularJS中使用Chart.js制折线图与饼图实例  Chart.js 是...
IE浏览器兼容性后续前言 继续尝试解决IE浏览器兼容性问题,...
Angular实现下拉菜单多选写这篇文章时,引用文章地址如下:h...
在AngularJS应用中集成科大讯飞语音输入功能前言 根据项目...
Angular数据更新不及时问题探讨前言 在修复控制角标正确变...