Require multiple controllers in a directive

5,802 views
Skip to first unread message

wingy

unread,
Aug 16, 2012, 10:10:54 AM8/16/12
to ang...@googlegroups.com
Is it possible to require multiple controllers to a link function in a directive?

Something similar to this:

  .directive('contents', function () {
    return {
      require: '^ctrl1, ^ctrl2',
      link: function (scope, iEl, iAttrs, ctrl1, ctrl2) {
        
      }
    };
  });

Wingy

wingy

unread,
Aug 16, 2012, 10:31:01 AM8/16/12
to ang...@googlegroups.com
This worked:

    require: ['^pane', '^panes'],

Could someone with authority please put this info in: http://docs.angularjs.org/guide/directive

Would be helpful for others!

Wingy

Salman Abbas

unread,
Nov 3, 2012, 3:38:02 AM11/3/12
to ang...@googlegroups.com, johnn...@yobistore.com
Thanks Wingy!

ProLoser

unread,
Nov 3, 2012, 3:48:43 AM11/3/12
to ang...@googlegroups.com, johnn...@yobistore.com
Does it add more controller parameters to the linking function? Or does the 1 controller parameter become an array?

Peter Bacon Darwin

unread,
Nov 3, 2012, 4:19:51 AM11/3/12
to ang...@googlegroups.com

The Ctrl param becomes a function. I got caught by this when I accidentally had a single require in an array.
Require: ['someCtrl']  -> Ctrl is array with one item

... sent from my tablet

--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular?hl=en.
 
 

Joshua White

unread,
Jul 25, 2013, 6:07:40 PM7/25/13
to ang...@googlegroups.com
I ran into a similar problem. I needed the current directive's controller as well as the parent directive. You can list multiple controllers in the require array then expect them to be injected as an array as the last element of the link function. Maybe there is a better way but it worked for me.

Here is the general idea in some coffescript:

directive "bgParent" ->
  controller: ParentController

directive "bgChild" ->
  controller: MyCtrl
  require:["bgChild", "^bgParent"]
  link:  
    return ($scope, $element, $attrs, controllers) ->
      myCtrl = controllers[0]
      parentCtrl = controllers[1]

<div bg-parent>
  <div bg-child/>
</div>
Reply all
Reply to author
Forward
0 new messages