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

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

Closed
rgraffconnect opened this issue Oct 18, 2013 · 8 comments
Closed

Comments

@rgraffconnect
Copy link

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' );.

@caitp
Copy link
Contributor

caitp commented Oct 20, 2013

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 pushed a commit to caitp/angular.js that referenced this issue Oct 20, 2013
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 angular#4518
@dtabuenc
Copy link
Contributor

dtabuenc commented Dec 2, 2013

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.

@caitp
Copy link
Contributor

caitp commented Dec 2, 2013

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)

@cphillips
Copy link

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"

@pkozlowski-opensource
Copy link
Member

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 ^^?).

caitp pushed a commit to caitp/angular.js that referenced this issue Feb 4, 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 angular#4518
@Narretz Narretz added this to the Backlog milestone Jun 24, 2014
@acollard
Copy link

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

@cphillips
Copy link

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
#4518 (comment).

@btford btford removed the gh: issue label Aug 20, 2014
@benbenwilde
Copy link

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

caitp pushed a commit to caitp/angular.js that referenced this issue Sep 5, 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 angular#4518
caitp pushed a commit to caitp/angular.js that referenced this issue Sep 5, 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 angular#4518
caitp pushed a commit to caitp/angular.js that referenced this issue Sep 9, 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 angular#4518
caitp pushed a commit to caitp/angular.js that referenced this issue Sep 22, 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 angular#4518
@caitp caitp closed this as completed in 07e3abc Sep 26, 2014
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants