[英]How to access both child and parent controller in directive?
We have two directives, called parent
and child
. 我们有两个指令,称为
parent
和child
。 Both of them have controllers defined, which hold some functionality. 它们都定义了控制器,它们具有一些功能。 For
child
directive we can: 对于
child
指令,我们可以:
parent controller
with require
property ( require: '^parent'
), thus receiving fourth parameter to our link function with its value: function link(scope, elem, attrs, parentCtrl)
require
属性访问parent controller
( require: '^parent'
),从而接收第四个参数到我们的链接函数及其值: function link(scope, elem, attrs, parentCtrl)
child controller
: without using require
, fourth link parameter will be our childController. child controller
:不使用require
,第四个链接参数将是我们的childController。 So the question is: how can we reference both child and parent controllers in child
's link function? 所以,问题是:我们如何能够引用两个孩子和家长控制器
child
的链接功能? Here's a plunker with the example: http://plnkr.co/edit/WzU6iJgf2zLLApFHeiEG?p=preview 这是一个带有示例的plunker: http ://plnkr.co/edit/WzU6iJgf2zLLApFHeiEG?p = preview
You can pass an array to the 'require' property of your directive definition, which includes both your child directive, and your parent directive. 您可以将数组传递给指令定义的'require'属性,该属性包括子指令和父指令。 The 4th parameter to your link function will be an array:
链接函数的第4个参数是一个数组:
app.directive('childDirective', function() {
require: ['childDirective', '^parentDirective'],
link: function(scope, element, attr, ctrls) {
var childCtrl = ctrls[0];
var parentCtrl = ctrls[1];
...
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.