gpt4 book ai didi

angularjs - Angular ,广播不起作用

转载 作者:行者123 更新时间:2023-12-04 02:50:24 26 4
gpt4 key购买 nike

我的目标是将一些数据从一个 Angular Controller 发送到另一个。

这是必须发送数据的 Controller :

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);
})
}

}]);

这是第二个 Controller
myApp.controller('ExcelViewCtrl', ['$scope', '$http', 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>

最佳答案

我认为您需要使用 $rootScope代替 $scope.$broadcast .见 JSFiddle 中的好例子

关于angularjs - Angular ,广播不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18247789/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com