AngularJS:指令之间的通信 [英] AngularJS: Communication between directives

查看:24
本文介绍了AngularJS:指令之间的通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个指令来创建一个 mp3/音频播放器.问题是您可以在一页中拥有多个音频播放器.我想做的是当一个正在播放而你开始另一个时,当前正在播放的暂停.如何使用角度指令实现这一点?

I'm writing a directive which creates an mp3/audio player. The issue is that you can have many audio players in one page. What I would like to do is when one is playing and you start an other, that the one currently playing pauses. How can I achieve this with angular directives?

提前致谢!

推荐答案

你也可以做 $rootScope.$broadcast 事件,例如 playerStarted.所有指令都可以订阅此事件,并且它们可以通过停止自己来对此事件做出反应.您需要做的一件事是传递有关正在启动的播放器的数据,以便新播放器不会停止自己,因为它也会订阅此类事件.

You can also do $rootScope.$broadcast events like playerStarted. This event can be subscribed by all directives and they can react to this event by stopping themselves. The one thing that you need to do would be pass in the data about the player which is starting so that the new player does not stop itself as it too would subscribe to such event.

这篇关于AngularJS:指令之间的通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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