Thursday, May 23, 2024
 Popular · Latest · Hot · Upcoming
126
rated 0 times [  132] [ 6]  / answers: 1 / hits: 24368  / 12 Years ago, sun, september 30, 2012, 12:00:00

How can I broadcast an object through an event?



Currently I am trying:



app.run ($rootScope) ->
message = {type: 'channel', action: 'create', data: { name: ssss, id: 0}}
$rootScope.$broadcast('message', message)

angular.module('WebChat').controller 'ChannelController', ($scope) ->
$scope.$on 'message', (message) ->
console.log message
console.log 'hi'


But I am getting no output



Edit
I got it working. It seems that the first parameter of the callback function is the scope. I had to change the controller to:



angular.module('WebChat').controller 'ChannelController', ($scope) -> 
$scope.$on 'message', (scope, message) ->
console.log message
console.log 'hi'

More From » angularjs

 Answers
0

You are getting no output in your case since you are broadcasting before a controller is ready to accept messages. The module's run method is executed very early in the application's life-cycle, before controller's scopes are ready to listen to messages.



Here is the jsFiddle that illustrates this, check the console to see that broadcast happens before a listener is ready: http://jsfiddle.net/vPq2P/3/


[#82829] Friday, September 28, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
billie

Total Points: 101
Total Questions: 114
Total Answers: 98

Location: Burundi
Member since Wed, Nov 25, 2020
4 Years ago
billie questions
;