我的目的是将一些数据从角度控制器发送到另一个.
控制器必须发送数据:
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>