简体   繁体   English

在angular指令中访问父控制器

[英]Access parent controller in angular directive

having same nested directives: 具有相同的嵌套指令:

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

if mydir requires ?^mydir it always get itself's controller. 如果mydir需要?^mydir则它总是获取其自身的控制器。
test_plunk test_plunk
is it possible to access parent's controller? 可以访问父母的控制器吗?

According the angular documentation on jqLite, you can call controller() to retrieve the controller for any given element: 根据jqLit​​e上的角度文档,您可以调用controller()来检索任何给定元素的控制器:

controller(name) - retrieves the controller of the current element or its parent. controller(name)-检索当前元素或其父元素的控制器。 By default retrieves controller associated with the ngController directive. 默认情况下,检索与ngController指令关联的控制器。 If name is provided as camelCase directive name, then the controller for this directive will be retrieved (eg 'ngModel'). 如果提供名称作为camelCase指令名称,则将检索该指令的控制器(例如'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: 在链接函数中,可以通过在父元素上调用controller()并传入指令名称来检索父控制器:

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

shall give you a 会给你a

$scope is the scope injected by angular $ scope是角度注入的范围

$parent is a keyword reference to the parent scope from any scope in angular. $ parent是从任何角度范围到父范围的关键字引用。

Yes this is JavaScript, and as was asked in the question, AngularJS powered Javascript 是的,这是JavaScript,正如问题所问,AngularJS支持Javascript

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM