angular.js icon indicating copy to clipboard operation
angular.js copied to clipboard

Can't require a parent directive of an element with the same directive.

Open rgraffconnect opened this issue 13 years ago • 8 comments

Just as the ngForm directive is able to be nested and communicate with parent ngForm controllers, there should be a clear method to optionally require a parent directive in the case where the current element contains the same directive. Currently, a require value of ^?myDir will result in the current controller being injected into the link function of the myDir directive. It would be very helpful if a require value such as ^^?myDir would ignore the current element/controller and use the same methodology used in the ngForm directive, effectively: element.parent().controller( 'myDir' );.

rgraffconnect avatar Oct 18 '13 14:10 rgraffconnect

repro -- It appears that require: '^' for same attribute will inject attributes own controller. This could be a problem for creating tree structures with the same directive. I'm not sure this is really a desirable behaviour and will look into it


It seems to be due to jqLite's implementation of inheritedData(), which will use the current node's data if there is any with the correct name. This is probably desirable for most uses of inheritedData(), but not necessarily this one.

caitp avatar Oct 20 '13 06:10 caitp

I have yet to run into anyone who has done ^myDir and wanted it to match on the current element. If it were up to me I'd just make the default behavior of ^myDir search exclusively starting from the parent node. This is consistent with what the current documentation says and would avoid adding extra ^^myDir syntaxes.

dtabuenc avatar Dec 02 '13 01:12 dtabuenc

I tend to agree, but I think it should wait for some proper review, as there may be some use case that I'm oblivious to that depends on this behaviour (as unlikely as that may be)

caitp avatar Dec 02 '13 01:12 caitp

I tend to agree with Daniel on this, however I have seen 3rd party documentation written like: http://www.cheatography.com/proloser/cheat-sheets/angularjs/

Where it says ^ means "Look on parent elements too"

cphillips avatar Dec 07 '13 13:12 cphillips

Yeh, I've also got use-cases where it would be desirable to omit controllers on the same elements. If there are concerns about breaking changes we could have alternative syntax that would allow us to explicitly say that we want only controllers from a parent element (maybe ^^?).

pkozlowski-opensource avatar Feb 04 '14 16:02 pkozlowski-opensource

It would be nice to get this fixed. IMO it should start with the parent. All the documentation states that '^' refers to parents.

acollard avatar Jul 17 '14 16:07 acollard

Yes, it would be, in the mean time you can get the parent controller like this. This hack seems pretty stable.

var cntrl = element.parent().inheritedData('$YOURCNTRLNAMEController')

where YOURCNTRLNAME is whatever you named your controller.

On Thu, Jul 17, 2014 at 9:38 AM, Andrew Collard [email protected] wrote:

It would be nice to get this fixed. IMO it should start with the parent. All the documentation states that '^' refers to parents.

— Reply to this email directly or view it on GitHub https://github.com/angular/angular.js/issues/4518#issuecomment-49331765.

cphillips avatar Jul 17 '14 17:07 cphillips

+1 this would be nice! Thanks for the workaround in the meantime..

benbenwilde avatar Aug 22 '14 23:08 benbenwilde