加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – 角色,广播不工作

发布时间:2020-12-17 09:17:19 所属栏目:安全 来源:网络整理
导读:我的目的是将一些数据从角度控制器发送到另一个. 控制器必须发送数据: myApp.controller('MapCtrl',['$scope','$http',function ($scope,$http) { $scope.loadData = function () { $http.get('/map/GetListDB').success(function (data,status,headers,con
我的目的是将一些数据从角度控制器发送到另一个.

控制器必须发送数据:

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方法.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读