简体   繁体   English

AngularJS:指令之间的通信

[英]AngularJS: Communication between directives

I'm writing a directive which creates an mp3/audio player. 我正在写一个创建mp3 /音频播放器的指令。 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? 如何通过角度指令实现这一目标?

Thanks in advance! 提前致谢!

Make a service that each directive uses and hold the state in there. 制作每个指令使用的服务并在其中保持状态。

Something like this: 像这样的东西:

angular.module('MyPlayer' [])
.factory('playerState', function() {
    var players = [];
    return {
        registerPlayer: function(player) {
            players.add(player);
        },
        unregisterPlayer: function(player) {
            var i = players.indexOf(player);
            (i>-1) && players.splice(i,1);
        },
        stopAllPlayers: function() {
            for(var i=0;i<players.length;i++) {
                players[i].stop();
            }
        }
    }
})
.directive('player', function(playerState) {
    return {
        ...
        link: function(scope, elem, attr) {
            var player = {
                stop: function() {
                    /* logic to stop playing */
                },
                play = function(song) {
                    playerState.stopAllPlayers();
                    /* logic to start playing */
                }
            }

            playerState.registerPlayer(player);
            scope.$on("$destroy", function() {
                playerState.unregister(player);
            });

            scope.play = player.play;
            scope.stop = player.stop;

            ...
        }
    }
})

Just to make the answers complete, next to broadcasting events, and exposing a service, you can also use directive controllers. 只是为了完成答案,在广播事件旁边,以及公开服务,您还可以使用指令控制器。 These controllers are set through the controller property of a directive definition object and are shared between directives that require the same controller. 这些控制器通过指令定义对象的controller属性设置,并在require相同控制器的指令之间共享。 This means you can have one controller for all the media players, where you can implement the logic you mentioned. 这意味着您可以为所有媒体播放器配备一个控制器,您可以在其中实现您提到的逻辑。 See the documentation on directives (search for controller: ) for more information. 有关更多信息,请参阅有关指令文档 (搜索controller: :)。

I would recommend the service approach if you think there will be more consumers of the logic, or the directive controller approach if only the directives consume the logic. 如果你认为会有更多的逻辑消费者,或者只有指令消耗逻辑的指令控制器方法,我会推荐服务方法。 I would advise against broadcasting events on the root scope because of the uncoupled and global nature of it. 我建议不要在根范围内广播事件,因为它具有非耦合性和全局性。 Just my two cents! 只是我的两分钱! HTH HTH

How are your directives setup? 您的指令如何设置? Please provide some code. 请提供一些代码。

This depends on the scope of your directives, I'm going to assume a child scope. 这取决于你的指令的范围,我将假设一个子范围。 To communicate between the directives, when a user clicked to start a player, I would call a $scope.$parent.$broadcast() - or $rootScope.$broadcast() if the directives are in different controllers or using isolated scopes, but then you need to inject $rootScope into your directive - to send an event to all child scopes. 要在指令之间进行通信,当用户点击启动播放器时,我会调用$ scope。$ parent。$ broadcast() - 或$ rootScope。$ broadcast()如果指令在不同的控制器中或使用隔离的作用域,但是你需要将$ rootScope注入你的指令 - 将事件发送到所有子范围。 My directives would be watching for this event using $on and any players that were playing would stop. 我的指令是使用$ on观看此活动,任何正在玩的玩家都会停止。 After this broadcast the player clicked would start. 在此广播之后,点击的玩家将开始。

$broadcast() and $on() scope documentation $ broadcast()和$ on()范围文档

You can also do $rootScope.$broadcast events like playerStarted . 您还可以执行$ rootScope。$ broadcast事件,如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. 你需要做的一件事就是传递关于正在开始的玩家的数据,以便新玩家不会停止,因为它也会订阅这样的事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM