maria maria - 2 months ago 17
AngularJS Question

$rootscope.$emit / $broadcast wont pass parameter

Im having the following:

$rootScope.$on('setmoney',function (thisdata) {

console.log("setting money");
console.log("money is : " + thisdata);
});


and this somewhere else:

$rootScope.$broadcast('setmoney', {cash: 100000});


How can i make sure i can retrieve the parameter cash in the setmoney rootscope.on function?

output after thisdata:

Object {name: "setmoney", targetScope: Scope, defaultPrevented: false, currentScope: Scope}
currentScope
:
null
defaultPrevented
:
false
name
:
"setmoney"
preventDefault
:
()
targetScope
:
Scope
__proto__
:
Object

Answer

The first parameter that $on gets is the event itself. After this first parameter, you get the rest of the parameters passed along with the event.

$rootScope.$on('setmoney',function (event, thisdata) { // You forgot to pass the 'event' parameter before the rest of parameters
    console.log("setting money");
    console.log("money is : " + thisdata);
});
Comments