javascript - 从外部文件到指令的 Angular 广播数据

标签 javascript angularjs angularjs-directive

我正在尝试将数据从外部 js 文件传递​​到指令 Angular 文件。 这就是我正在做的事情。

在我的外部 js 文件上:

var formatData = {
    id: 1,
    state: 'moving'
}    
angular.element(document).scope().$broadcast('sendData', formatData); 

在我的指令中:

scope.$on('API.sendData', function(formatData) { 
    console.dir(formatData);
});

记录了什么: enter image description here

帮忙?

最佳答案

您应该使用监听器函数的第二个参数,第一个参数是事件数据。

scope.$on('API.sendData', function(event, formatData) { 
    console.dir(formatData);
});

希望这有帮助。

关于javascript - 从外部文件到指令的 Angular 广播数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29666835/

相关文章:

javascript - 类型错误 : Cannot call method '$on' of undefined

javascript - Node.js 服务器一直在加载,但没有结果。

javascript - 在 Node.js 中查找 GPU 信息(模型)

javascript - AngularJS : can not change parent scope via directive

html - 什么元素在中心?

javascript - AngularJS alpha only 正则表达式接受特殊字符

javascript - 现场过滤数据的最佳方法

javascript - 在不知道 sessionStorage 中的键名称的情况下访问 JSON 对象的元素

javascript - 为 AngularJS 应用加载本地化内容

javascript - 修复打开和关闭 Angular ui Bootstrap 的 uib-modal 时的页面移位