定时器的指令通讯的角度指令 [英] Angular directive to directive communication for timer

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

问题描述

我从

I have taken the ideas from AngularJS - How to make a stop watch starting from 00:00:00 format and have a need to have the timer be able to have a stop method go up to the parent directive for a button that is in the parent template.

<parent-container>
  <timer-container />
  <button ng-click="??.stopTimer()">Stop</button>
</parent-container>

如果在计时器容器中有一个方法可以停止计时器,如何使父容器和计时器容器进行交互,以便在单击按钮时将调用stopTimer方法?

If there is a method in the timer-container that will stop the timer, how do I get the parent container and timer container to interact so that when the button is clicked, the stopTimer method will be invoked?

推荐答案

查看 $ emit $ broadcast 函数.$ emit将在层次结构中发送事件,而$ broadcast将在层次结构中发送事件.您可以使用 $ on 函数添加侦听器.

Look into the $emit and $broadcast functions. $emit will send an event up the heirarchy and $broadcast sends the event down the heirarchy. You can add a listener using the $on function.

如果两个作用域之间没有直接关系,则可以将$ rootScope注入两个控制器中,并在那里处理事件.

If there is no direct relation between the two scopes, you can inject $rootScope into both controllers and handle the event there.

有关更完整的示例,请检查我在评论中链接的答案.

For a more complete example, check the answer I linked in my comment.

这篇关于定时器的指令通讯的角度指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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