在角指令访问父控制器 [英] Access parent controller in angular directive

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

问题描述

有相同的嵌套指令:

<div mydir="a">
  <div mydir="b">
  </div>
</div>

如果 MYDIR 要求?^ MYDIR 都能得到自己的控制器。结果
test_plunk 结果
是否有可能访问家长的控制器?

if mydir requires ?^mydir it always get itself's controller.
test_plunk
is it possible to access parent's controller?

推荐答案

据上jqLit​​e角文档,可以呼叫控制器()来检索任何给定的元素控制器:

According the angular documentation on jqLite, you can call controller() to retrieve the controller for any given element:

控制器(名) - 检索当前元素或其母公司的控制器。
  默认情况下检索与ngController指令相关的控制器。
  如果名称是作为驼峰指令名,那么控制器此
  指令将被检索(如'ngModel')

controller(name) - retrieves the controller of the current element or its parent. By default retrieves controller associated with the ngController directive. If name is provided as camelCase directive name, then the controller for this directive will be retrieved (e.g. 'ngModel').

在你的链接功能,您可以通过调用父元素上的控制器()并传入该指令的名称检索父控制器:

Within your link function, you can retrieve the parent controller by calling controller() on the parent element and passing in the name of the directive:

var parentCtrl = iElement.parent().controller('mydir');

这篇关于在角指令访问父控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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