在不注入 $rootScope 的情况下对指令中的角度事件做出反应 [英] Reacting on angular events in a directive without injecting $rootScope
问题描述
我想知道您是否有一个指令代码示例,它可以对 $routeChangeError
之类的角度事件做出反应,而无需将 $rootScope
注入其中(使用 $
在链接函数中).在我看来,它打破了 MV* 模式并产生"了气味代码(提供了在指令中操纵根范围的可能性).提前致谢.
I wonder if you have an example of a directive code that reacts on angular events like $routeChangeError
without injecting $rootScope
in to it (to use $on
in link function). It breaks in my opinion MV* pattern and "produces" smell-code (gives the possibility to manipulate root scope in a directive). Thanks in advance.
推荐答案
如果你只是监听事件,则不必使用$rootScope
;做例如$scope.$on("$routeChangeError")
在指令的范围内,来自控制器或链接函数.
If you are only listening for events, you don't have to use the $rootScope
; do e.g. $scope.$on("$routeChangeError")
on the scope of the directive, from either controller or link function.
你看到 "$routeChangeError"
是从 $rootScope
广播的,所以所有孩子都会收到它.
You see the "$routeChangeError"
is broadcasted from the $rootScope
, so all children receive it.
这篇关于在不注入 $rootScope 的情况下对指令中的角度事件做出反应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!