我的目的是将一些数据从角度控制器发送到另一个.
控制器必须发送数据:
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消息将被路由.
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.jb51.cc/angularjs/140708.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。