Undefined Directive Controller

36 views
Skip to first unread message

César Costa

unread,
Oct 11, 2014, 2:56:11 PM10/11/14
to ang...@googlegroups.com
Hi,

I implemented a parent directive that have two child directives. When the user click in button inside child-one directive i need to toggle a content from child-two directive. But, the parent controller inject on childs directive are undefined. 

This plunker show my case:


Can anyone help me?


Justin Walsh

unread,
Oct 12, 2014, 6:56:20 AM10/12/14
to ang...@googlegroups.com
Hi César

In your child directives, replace:

    parent: '^parent',

with:

    require: '^parent',

Regards
Justin

César Barone

unread,
Oct 12, 2014, 8:56:41 AM10/12/14
to ang...@googlegroups.com
Oh god. :p. Thanks Justin.

Why angular doesn't show any error?

I replaced and  i can't toggle child-two. I updated my plunk:




--
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/UIyBJJSQjGs/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.

César Barone

unread,
Oct 12, 2014, 9:06:53 AM10/12/14
to ang...@googlegroups.com
Hi, i updated my plunk again and this is my solution:


Is it the correct solution for this problem?

Thanks :)

Justin Walsh

unread,
Oct 12, 2014, 12:37:44 PM10/12/14
to ang...@googlegroups.com
Hi, 

The reason you didn't get an error with 

parent:"^parent"

Is that JavaScript allows the dynamic creation of properties on an object, and in your case, you just created one with the name: parent.  This isn't an error by - and angular just went and checked for a 'require' property and didn't find one (since it optional, this is a valid scenario).

In terms of the 'right' way to do this, a simple solution is this plnkr.  Here, the child directive shares the scope with the parent and so the property (show) is visible from the child. You may however want to
  • Introduce a dot in the model (i.e. $scope.model.show = true).  This angular wiki page will provide some understanding.
  • Introduce isolate scope in your directive (depending on your usage). 
Regards
Justin

César Barone

unread,
Oct 14, 2014, 10:51:36 AM10/14/14
to ang...@googlegroups.com
Hi,

First, thanks Justin.

I understand why angular can't throw any error when i create key 'parent'. But, i don't understand why angular doesn't validates unknown keys and throws an error.

About the directive. I isolated the scope of my parent directive to avoid show/hide all child-two directives, when i have more than one of it. But, now, the text of directive child-two is never show, because  child.show  is undefined.

I think that child-two prototypical inherits  scope from parent scope and have 'direct' access to parent properties. Why child.show is undefined? This is my updated plunk.



Reply all
Reply to author
Forward
0 new messages