Javascript 在 Angularjs 服务中监听窗口事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28382927/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Listen to window events in an Angularjs service
提问by Sojharo Mangi
I want to listen to the window events in my AngularJS service so that I can broadcast them to my controllers.
我想在我的 AngularJS 服务中监听窗口事件,以便我可以将它们广播到我的控制器。
I have a Chrome extension which sends any message using port.postMessage('Any Message');.
我有一个 Chrome 扩展程序,它使用port.postMessage('Any Message');.
I want my angularjs service to listen to that message and send it to the controller using $rootScope.$broadcast("Something occurred.");
我希望我的 angularjs 服务侦听该消息并将其发送到控制器使用 $rootScope.$broadcast("Something occurred.");
Inside my service, I am trying to do so with the following listener.
在我的服务中,我正在尝试使用以下侦听器执行此操作。
window.addEventListener('Any Message', function (event) {
if (event.origin != window.location.origin) {
return;
}
$rootScope.$broadcast("Something occurred.");
});
I also tried $windowbut I don't know why the above code does not work. Also my IDE, jetbrains webstorms classify above code snippet as unreachable.
我也试过,$window但我不知道为什么上面的代码不起作用。还有我的 IDE,jetbrains webstorms 将上面的代码片段归类为无法访问。
Before this, I used the above code in a controller and it worked fine. I wasn't doing broadcast in controller. Now I want to move this to the service so that all controllers should be able to listen to it from service.
在此之前,我在控制器中使用了上面的代码并且运行良好。我没有在控制器中进行广播。现在我想把它移到服务中,这样所有的控制器都应该能够从服务中监听它。
回答by shershen
Here is working example I've made - with subscribing DOM event and broadcasting the event from service to controller: http://plnkr.co/edit/nk2aPt?p=preview
这是我制作的工作示例 - 订阅 DOM 事件并将事件从服务广播到控制器:http: //plnkr.co/edit/nk2aPt?p=preview
//this is service that creates subscription
app.service('serviceName', function($window, $rootScope) {
function subsFunc() {
$window.addEventListener('click', function(e) {
$rootScope.$broadcast('app.clickEvent', e);
})
}
return {
"subscribeMe": subsFunc }
});
//this will be in controller
$rootScope.$on('app.clickEvent', function(a, b) {
//a,b - event object details
});
回答by Rian
Something like this might help:
像这样的事情可能会有所帮助:
var app = angular.module('app', []);
app.run(["$window", "$rootScope", function($window, $rootScope) {
$window.addEventListener('message', function(e) {
$rootScope.$broadcast('message', e.data);
});
} ]);
回答by vishnu das
Problem:
问题:
"$window listener" listen multiple time.
Example:
示例:
Consider , we wrote a listener code in Page B. My starting page is Page A.
考虑一下,我们在页面 B 中编写了一个侦听器代码。我的起始页是Page A。
I added an alert in listener code in Page B.
我在Page B 的侦听器代码中添加了一个警报。
$window.addEventListener('message', function (e) { alert() });
Lets Go,
我们走吧,
- Page A >> Page B (alert count 1) Page B >> Page A >> Page B (alert count 2) Page B >> Page A >> Page B (alert count 3)
- 页 A >> 页 B(警报计数 1) 页 B >> 页 A >> 页 B(警报计数 2) 页 B >> 页 A >> 页 B(警报计数 3)
回答by AdirAmsalem
Do you inject that service somewhere? Services will run only if you really injects them.
您是否在某处注入该服务?服务只有在您真正注入它们时才会运行。
Additionally, it's better to use $window instead of window, so you'll be able to mock it later in your tests.
此外,最好使用 $window 而不是 window,这样您就可以在稍后的测试中模拟它。

