如何创建在AngularJs服务自定义事件 [英] How do I create a custom event in an AngularJs service

查看:127
本文介绍了如何创建在AngularJs服务自定义事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个项目AngularJs。我有它设置,并删除一些按键事件的服务。这项服务是由我不想直接与按钮互动另一个服务使用。不过,我想通过第一服务进行过滤了一个按钮单击事件,并在第二个处理。由于我不希望第二个服务要注意的按钮,我想我需要创建的第一个服务的自定义事件。如何创建一个自定义事件和火是单击按钮时?

I am working on an AngularJs project. I have a service which sets and removes events on some buttons. This service is utilized by another service which I do not want to interact directly with the buttons. However I would like a button click event to be filtered up through the first service and handled in the second one. Since I don't want the second service to be aware of the buttons, I figure I will need to create a custom event in the first service. How can I create a custom event and fire it when a button is clicked?

先谢谢了。

推荐答案

如果您想发送的事件服务/指令使用广播

If you want to send an event between services/directives use broadcast:

$rootScope.$broadcast('buttonPressedEvent');

和接受她这样的:

$rootScope.$on('buttonPressedEvent', function () {
             //do stuff
        })

这篇关于如何创建在AngularJs服务自定义事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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