如何从 angular.js 中的服务 $rootScope.$broadcast
在 $rootScope.$broadcast 之后,我想从 Controller 进行 console.log(data),但我在控制台中看不到任何内容
我的代码:
'use strict';
angular.module('adf.widget.charts')
.factory('chartService', function ($q, $rootScope){
return {
getSpreadsheet: function init(path){
var deferred = $q.defer();
Tabletop.init({
key: path,
callback: function(data, Tabletop) {
$rootScope.$broadcast(data)
},
simpleSheet: true,
debug: true
});
}
}
})
.controller('piechartCtrl', ['$scope', 'chartService', function($scope, chartService, urls) {
$scope.$on('getSpreadsheet', function(data){
console.log(data)
});
}]);
最佳答案
您$rootScope.$broadcast
应该将第一个参数设置为事件名称,然后您可以将数据作为第二个参数传递给该事件。
$rootScope.$broadcast('getSpreadsheet', data)
关于javascript - 如何从 angular.js 中的服务进行 $rootScope.$broadcast,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34814238/