简体   繁体   English

Angular JS指令特定的控制器和父控制器

[英]Angular JS directive specific controller & parent controller

I would like to use a directive specific controller and the parent controller in the link function. 我想在链接函数中使用指令专用控制器和父控制器。

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
              }
           }
}

Is there a way I can use directiveSpecificController but also have access to the parent controller? 有没有一种方法可以使用DirectiveSpecificController但也可以访问父控制器?

Yes, you just need to require your own controller too: 是的,您也只需要自己的控制器即可:

http://plnkr.co/edit/2x7yxRfJWqXi1FfZmb3V?p=preview 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);
    }
  }
})

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

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