Imo
Imo

Reputation: 1475

How to $rootScope.$broadcast from services in angular.js

How can I $rootScope.$broadcast from services in angular.js

after $rootScope.$broadcast I would like to console.log(data) from controller but I do not see anything in the console

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

  }]);

Upvotes: 0

Views: 187

Answers (2)

Charlie
Charlie

Reputation: 23858

Here is the $broadcast syntax:

$broadcast(name, args);

The name argument represents the name of the event that should be triggered down the line of child scopes.

Example:

$broadcast('testEvent', 'test data');

$on('testEvent', function(arg){
   console.log(arg);   //This will log 'test data'
})

Upvotes: 0

Pankaj Parkar
Pankaj Parkar

Reputation: 136184

You $rootScope.$broadcast should have first parameter be the event name & there after you could pass data to that event as 2nd parameter.

$rootScope.$broadcast('getSpreadsheet', data)

Upvotes: 1

Related Questions