【问题标题】:$rootscope.$emit / $broadcast wont pass parameter$rootscope.$emit / $broadcast 不会传递参数
【发布时间】:2016-09-30 10:04:14
【问题描述】:

我有以下几点:

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

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

还有这个:

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

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

此数据后的输出:

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

【问题讨论】:

    标签: angularjs broadcast rootscope


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-09
      • 1970-01-01
      • 2015-02-28
      • 2018-03-16
      • 1970-01-01
      相关资源
      最近更新 更多