找到你要的答案

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

Q:利用多角js指令在同一范围但侦听来自特定元素的特定事件

Hi I am using this angular countdown plugin

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

and I need to use the directive multiple times within the same scope. The plugin broadcasts a 'timer-tick' event which I want to listen for but only from a specific directive.

Whats the best approach to this problem? I only seem to be getting the event broadcasted for the first instantiation of the element. In my case it has an $id of 38.

Here is the code in my controller

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

HTML for first timer directive

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

HTML for second timer directive

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

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

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

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

解决这个问题的最好方法是什么?我似乎得到的事件广播为元素的第一个实例。在我的情况下,它有一个$ 38的ID。

这是我控制器中的代码

   $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> 
answer1: 回答1:

See the code at http://siddii.github.io/angular-timer/examples.html#/angularjs-polling-timer I think they've have an example of what you're trying to do.

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

See the code at http://siddii.github.io/angular-timer/examples.html#/angularjs-polling-timer I think they've have an example of what you're trying to do.

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