angularjs – 角色,广播不工作

前端之家收集整理的这篇文章主要介绍了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方法.

原文链接:https://www.f2er.com/angularjs/140708.html

猜你在找的Angularjs相关文章