在运行时将一个指令与一堆其他指令进行通信(Communication of one directive with a bunch of others at runtime)

编程入门 行业动态 更新时间:2024-10-11 01:20:28
在运行时将一个指令与一堆其他指令进行通信(Communication of one directive with a bunch of others at runtime)

我创建了一个'A'指令控件。 页面上会有不止一个。 页面上还有一个控件'B',它需要与'A'类型的指令进行通信,但一次只能有一个,即选择的那个(如该控件中的播放/停止视频)。 自从对angularjs世界不熟悉以来,我不确定它是否有可能做到这一点以及如何做到这一点。 我将不胜感激任何帮助。

谢谢,Imad。

I have created a 'A' directive control. There would be more than one of those on page. There is an other control 'B' on the page that would need to communicate with 'A' type of directives but only one at a time i.e., the one which is selected (like play/stop video in that control). Since new to angularjs world, I am not really sure if its possible to do this and how should it be done. I would appreciate any help.

Thanks, Imad.

最满意答案

主要问题是 - B指令中包含的所有A指令是什么?

如果答案是肯定的,你可以在B的控制器中注册A指令,就像在选项卡的angular的例子中一样:

http://jsfiddle.net/api/post/library/pure/

需要注意的部分是他们如何使用require: '^tabs'在A的指令中访问B的控制器。 在您的使用案例中,我想一旦选择了A实例,它应该在其链接函数中告诉B的控制器。

如果答案为否,则可以创建一个服务(例如,称为S),其中两个指令将通过该服务进行通信。 这次,B将在S中注册更改。 当选择A实例时,它会告诉它服务S,并且应该调用B的回调。

The main question is - are all the A directives contained in a B directive?

If the answer is yes, you can register the A directives in B's controller, like in angular's example for tabs:

http://jsfiddle.net/api/post/library/pure/

The part to note there is how they use require: '^tabs' in A's directive to access B's controller. In your use case, I guess that once an A instance is selected, it should tell it to B's controller in its link function.

If the answer is no, you can create a service (called S for example) in which the two directives will communicate through. This time, B will register in S for changes. When an A instance is selected, it will tell it to service S, and B's callback should be invoked.

更多推荐

本文发布于:2023-08-01 03:22:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1354190.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:指令   时将   通信   Communication   bunch

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!