我怎么能听在AngularJS jQuery的事件 [英] How can I listen to jQuery events in AngularJS

查看:106
本文介绍了我怎么能听在AngularJS jQuery的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我重写一个jQuery应用程序使用AngularJS。我使用的小步骤,一次更换一个领域。但是我有我如何能听jQuery的事件AngularJS的问题。

I am rewriting a jQuery application to using AngularJS. I am using small steps, and replacing one area at a time. I am however having issues on how I can listen to jQuery's events in AngularJS.

作为一个例子,我有一个的$(document).trigger(DoSomething的')在一个jQuery的Widget。我怎么能听它在AngularJS控制器?

As an example I have a $(document).trigger('doSomething') in a jQuery Widget. How can I listen to it in a AngularJS controller?

我已经尝试使用 $范围。在$('DoSomething的',函数(){}),但这只是指控制的范围。我也试图与 $ rootScope。在$('DoSomething的',函数(){}),但没有运气。

I have tried using $scope.$on('doSomething', function(){}), but that only refers to the scope of the controller. I also tried with $rootScope.$on('doSomething', function(){}), but without luck.

有什么办法,我一步一步可以转换我的应用程序,只听的jQuery触发文档事件?

Is there any way where I step by step can convert my application, and just listen to the document events triggered by jQuery?

推荐答案

我是从臀部射击在这里,但我觉得jQuery的事件和AngularJS事件是两个独立的系统。一个快速的解决方案可能是有一个全球性包罗万象的jQuery的事件监听器转换了jQuery事件到AngularJS事件。

I'm firing from the hip here, but I think jQuery events and AngularJS events are two separate systems. A quick solution might be to have a global catch-all jQuery event listener which translates the jQuery event into an AngularJS event.

后来你可能会想要两个包裹部件的指令,把一个更具体的事件翻译里面。在这一点上,你也可以,如果你想坚持到事件系统,或者角度的服务可能是小部件与世界其他国家交流的更合适的方式考虑。

Later on you might want two wrap the widget in a directive and put a more specific event-translator inside it. At that point you could also consider if you want to stick to the event-system or if an angular service might be a more suitable way for the widget to communicate with the rest of the world.

以上假设你不能或不想改写使用AngularJS机制部件。如果这是一个选项,然后只需使用 $范围。$广播 $范围。$发出直接在窗口小部件,而不是 $。触发

The above assumes you can't or don't want to rewrite the widget to use AngularJS mechanisms. If that's an option then just use $scope.$broadcast or $scope.$emit directly in the widget instead of $.trigger.

这篇关于我怎么能听在AngularJS jQuery的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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