[英]How to $rootScope.$broadcast from services in angular.js
How can I $rootScope.$broadcast from services in angular.js 我如何从angular.js中的服务$ rootScope。$ broadcast
after $rootScope.$broadcast I would like to console.log(data) from controller but I do not see anything in the console 在$ rootScope。$ broadcast之后,我想从控制器进行console.log(data),但控制台中什么都看不到
my code: 我的代码:
'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)
Here is the $broadcast
syntax: 这是
$broadcast
语法:
$broadcast(name, args);
The name
argument represents the name of the event that should be triggered down the line of child scopes. name
参数表示应该在子作用域下触发的事件的名称。
Example: 例:
$broadcast('testEvent', 'test data');
$on('testEvent', function(arg){
console.log(arg); //This will log 'test data'
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.