在同一$scope中使用多个Angular JS指令,但侦听来自特定元素的特定事件

Using multiple Angular JS directives in same $scope but listening for a specific event from a particular element

本文关键字:元素 事件 指令 JS scope Angular      更新时间:2023-09-26

嗨,我正在使用这个角度倒计时插件

http://siddii.github.io/angular-timer/

并且我需要在同一范围内多次使用该指令。该插件广播一个"计时器滴答"事件,我想听听,但只能从特定的指令。

解决这个问题的最佳方法是什么?我似乎只是在为元素的第一个实例化广播事件。就我而言,它的id为38美元。

这是我的控制器中的代码

   $scope.$on('timer-tick', function (event, data){
            console.log('Timer is ticking = ', data,'event : ',event,'scope ',$scope);
});

第一个定时器指令的HTML

 <span class="chat-time"><timer   interval="1000" countdown="currentEvent.duration"> {{hhours}} : {{mminutes}} : {{sseconds}} </timer></span> <!-- event duration for host -->

第二个定时器指令的HTML

<timer  finish-callback="setUserStatus(true)"  interval="1000" countdown="speed_roommating.session_clock"> {{hhours}} : {{mminutes}} : {{sseconds}} </timer> 

请参阅http://siddii.github.io/angular-timer/examples.html#/angularjs-轮询计时器我想他们已经有了一个你想做什么的例子。

$scope.$on('timer-tick', function (event, args) {
    $scope.timerConsole += $scope.timerType  + ' - event.name = '+ event.name + ', timeoutId = ' + args.timeoutId + ', millis = ' + args.millis +''n';
});