我想在链接函数中使用指令特定的控制器和父控制器。
module.directive('parent', function() {
return {
...
controller: SomeFunction
}
}
module.directive('child', function() {
return {
...
require('^parent'),
controller: SomeOtherFunction,
link: function(scope, element, attr, ctrl) {
//ctrl is the parent controller not the SomeOtherFunction
}
}
}
有没有办法可以使用directiveSpecificController但是还可以访问父控制器?
答案 0 :(得分:2)
是的,您只需要自己的控制器:
http://plnkr.co/edit/2x7yxRfJWqXi1FfZmb3V?p=preview
app.directive('parent', function() {
return {
controller: function() {
this.secret = 'apples';
}
}
})
app.directive('child', function() {
return {
controller: function() {
this.secret = 'oranges';
},
require: ['child', '^parent'],
link: function(scope, elem, attrs, ctrls) {
var parentCtrl = ctrls[1];
var childCtrl = ctrls[0]
console.log(parentCtrl.secret);
console.log(childCtrl.secret);
}
}
})