how can I identify parent directives inside of their children?

81 views
Skip to first unread message

xrd

unread,
Jul 9, 2014, 4:25:55 PM7/9/14
to ang...@googlegroups.com
Is there a way to identify the instance of each directive? 

I want to build a set of directives where child directives "know" the name of their parent directives. I was envisioning knowing the parent ID and then $broadcast'ing the ID. Then, inside my child directive, I was assuming if I listened for that even using $on, that I could retrieve the ID and associate it. Clearly I don't understand the directive lifecycle and instantiation.

I built a little plunkr to explain:

http://plnkr.co/edit/QmkzHXeyN1l18LVBgZe8?p=preview

My desired output would be to see the same ID for the first three and then another ID for the next three. For example:

foo (parent: 002)
foo (parent: 002)
foo (parent: 002)
foo (parent: 006)
foo (parent: 006)
foo (parent: 006)

Any ideas?

Chris

Sander Elias

unread,
Jul 10, 2014, 12:57:53 AM7/10/14
to ang...@googlegroups.com
Hi Chris,

Have a look at it. You probably have questions after that, don't hesitate to ask them here!

Regards
Sander

Chris Dawson

unread,
Jul 10, 2014, 3:18:09 AM7/10/14
to ang...@googlegroups.com
This is perfect. Thanks Sander!


--
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/LpHjmXkwb6Y/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.



--
Chris Dawson
971-533-8335
Human potential, travel and entrepreneurship:  http://webiphany.com/
Check out Teddy Hyde, Android editor for GitHub (especially Jekyll blogs): bit.ly/16BL13z

Roman

unread,
Jul 10, 2014, 7:21:16 AM7/10/14
to ang...@googlegroups.com
have you considered using a 'pre' link function in the parent so that you can populate the scope before the child directives fire?

Chris Dawson

unread,
Jul 14, 2014, 7:12:50 PM7/14/14
to ang...@googlegroups.com
Hey guys,

This works great for setting the ID into the children. Now I'm stuck trying to figure out how to save the ID into the parent as well. In either of your examples setting a variable on the scope sets it for all instances of the directive. 


Is there a way to save the ID into the scope of the parent as well? In other words, to have the "parent is ..." from the plinkr match the children?

Chris


--
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/LpHjmXkwb6Y/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Sander Elias

unread,
Jul 15, 2014, 2:08:59 AM7/15/14
to ang...@googlegroups.com
Hi Chris,

You do realize that using transclusion, the parent scope of your <foo> elements is the same parent scope? 

Regards
Sander

Chris Dawson

unread,
Jul 15, 2014, 12:58:32 PM7/15/14
to ang...@googlegroups.com
Sander,

I must not understand your summary properly. If the parent scope of "foo" is the same parent scope (meaning the scope of "bar"?) then why are both noted as "bar2" rather than the first set of "foo"s saying "bar1" and the second set of "foo"s saying "bar2"?

I've been using Batarang and trying to look at the enclosing scopes to understand what is happening, but not making much headway. 

Thank you for your help.

Chris


--
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/LpHjmXkwb6Y/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

xrd

unread,
Aug 16, 2014, 1:30:59 AM8/16/14
to ang...@googlegroups.com
Sander,

The reason I was asking these questions is because I am building a comic book markup language. Does this help clarify why and how I want to do the things I do?


An example is here:


The GitHub repository is here:


Chris


On Tuesday, 15 July 2014 09:58:32 UTC-7, xrd wrote:
Sander,

I must not understand your summary properly. If the parent scope of "foo" is the same parent scope (meaning the scope of "bar"?) then why are both noted as "bar2" rather than the first set of "foo"s saying "bar1" and the second set of "foo"s saying "bar2"?

I've been using Batarang and trying to look at the enclosing scopes to understand what is happening, but not making much headway. 

Thank you for your help.

Chris
On Mon, Jul 14, 2014 at 11:08 PM, Sander Elias <sande...@gmail.com> wrote:
Hi Chris,

You do realize that using transclusion, the parent scope of your <foo> elements is the same parent scope? 

Regards
Sander

--
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/LpHjmXkwb6Y/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+unsubscribe@googlegroups.com.

To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Sander Elias

unread,
Aug 16, 2014, 2:06:00 AM8/16/14
to ang...@googlegroups.com
Hi Chris,

Hmm, let me see if I get this. You want you parent to 'know' the children it has right?
There are quite a few ways to handle this.
Have a look at this adapted plunk:http://plnkr.co/edit/X6U1udLgAItRF8edSlaS?p=preview

Does that help? If not, try to 'plunk up' the issue!

Regards
Sander
Reply all
Reply to author
Forward
0 new messages