Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Directive unable to get parent controller if it is the same directive type #8240

Closed
acollard opened this issue Jul 17, 2014 · 1 comment
Closed

Comments

@acollard
Copy link

When nesting the same directive it is not possible to get the parent directives controller.

<concat>
    <concat>
    </concat>
</concat>
testApp.directive('concat', function () {
    return {
        require: ['concat', '?^concat'],
        controller: function () {},
        link: function (scope, elem, attrs, cntrls) {
            var cntrl = cntrls[0];
            var parent = cntrls[1]; 
            // (parent === cntrl) = true
        }
    };
});

Here is a full JSFiddle: http://jsfiddle.net/R2Ycd/2/

The issue is in the following line in getControllers function in compile.js.

value = value || $element[retrievalMethod]('$' + require + 'Controller');

The inheritedData retrieval method will look locally first. It should start with the parent first since '^' was specified.

if(retrievalMethod == 'inheritedData'){
  var parent =  $element.parent();
  if(parent)
    value = parent[retrievalMethod]('$' + require + 'Controller');
}
else
  value = value || $element[retrievalMethod]('$' + require + 'Controller');
@Narretz
Copy link
Contributor

Narretz commented Jul 17, 2014

Hi, this is a duplicate of #4518 and there's a pull for allowing this in #4540. If you post in these issues, it will (slightly) enhance the chance that they will be merged. In the meantime you can use element.parent().controller( 'myDir' );. or a variant of that.

@Narretz Narretz closed this as completed Jul 17, 2014
caitp pushed a commit that referenced this issue Sep 26, 2014
Implement option to strengthen require '^' operator, by adding another '^'.

When a second '^' is used, the controller will only search parent nodes for the
matching controller, and will throw or return null if not found, depending on
whether or not the requirement is optional.

Closes #4518
Closes #4540
Closes #8240
Closes #8511
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants