如何使用$ broadcast发送对象? [英] How can I send an object with $broadcast?

查看:72
本文介绍了如何使用$ broadcast发送对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下内容:

$scope.$watch('tableForm.$pristine', function (newValue) {
    $rootScope.$broadcast("tableDataUpdated", 
        { state: $scope.tableForm.$pristine });
});

我也尝试过:

$scope.$watch('tableForm.$pristine', function (newValue) {
    var tableForm = { pristine: $scope.tableForm.$pristine };
    $rootScope.$broadcast("tableDataUpdated", tableForm);
});

当tableForm $pristine状态改变时,$scope.tableForm.$pristine的值将设置为False,并广播此消息.

When the tableForm $pristine state changes then the value of $scope.tableForm.$pristine is set to False and this message is broadcast.

但是,当我尝试接收消息时,未定义状态"的值:

However when I try to receive the message the value of "state" is not defined:

$rootScope.$on("tableDataUpdated", function (args) {
    alert(args.state);
});

我也尝试过:

$rootScope.$on("tableDataUpdated", function (args) {
    alert(args.tableForm);
});

仍然似乎无法发送对象并接收到对象

Still I seem not to be able to send the object and have it received

推荐答案

这是因为侦听器函数有两个参数传递给它,分别是eventargs.请参见角度文档.

That's because listener function has two arguments being passed into it, event, and args. See the angular docs.

尝试:


$rootScope.$on("tableDataUpdated", function (event, args) {
        alert(args.state);
    });

这篇关于如何使用$ broadcast发送对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆