$ rootscope.$ emit/$ broadcast不会传递参数

mar*_*ria 2 broadcast angularjs rootscope

我有以下内容:

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

            console.log("setting money");
            console.log("money is : " + thisdata);
});
Run Code Online (Sandbox Code Playgroud)

而在其他地方:

 $rootScope.$broadcast('setmoney', {cash: 100000});
Run Code Online (Sandbox Code Playgroud)

我怎样才能确保我能在setmoney rootscope.on函数中检索参数现金?

此数据后的输出:

Object {name: "setmoney", targetScope: Scope, defaultPrevented: false, currentScope: Scope}
currentScope
:
null
defaultPrevented
:
false
name
:
"setmoney"
preventDefault
:
()
targetScope
:
Scope
__proto__
:
Object
Run Code Online (Sandbox Code Playgroud)

小智 6

获得的第一个参数$on是事件本身.在第一个参数之后,您将获得与事件一起传递的其余参数.

$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);
});
Run Code Online (Sandbox Code Playgroud)