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

时间:2016-09-30 10:04:14

标签: angularjs broadcast rootscope

我有以下内容:

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

以及其他地方:

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

如何确保我可以在setmoney rootscope.on函数中检索参数cash?

在此数据之后输出:

android:visibility="@{user.adult ? View.VISIBLE : View.GONE}"

public boolean isAdult() {
    return age >= 18;
}

1 个答案:

答案 0 :(得分:5)

$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);
});
相关问题