Nested angular directives using controller scoped variable

27 views
Skip to first unread message

Jonathan Matthew Beck

unread,
Jul 23, 2014, 2:59:29 PM7/23/14
to ang...@googlegroups.com
I am adapting JSONEdit (https://github.com/mb21/JSONedit), which uses Angular recursion (having directives create copies of themselves and call $compile()) to build a tree of nested directives from JSON.

When I toggle a variable at controller scope level I want for a ng-show="" to be toggled in each directive.

Here is the Plunker.  You may look at directives.js line 245.  This ng-show="" statement should should text in the tree when the "Toggle Value" button is pressed.  I even debugged this stepping up the scope.$parent.$parent and it never leads back to the controller.

http://plnkr.co/edit/Na9Beq

Jonathan Matthew Beck

unread,
Jul 23, 2014, 3:30:25 PM7/23/14
to ang...@googlegroups.com
Slightly updated Plnkr:
plnkr.co/edit/ULrPP1



--
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/ltScZz-c1Fk/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 24, 2014, 2:47:51 AM7/24/14
to ang...@googlegroups.com

Hi Jonathan,

Somehow I could not save my updated plunk..
Well,it look like you misunderstood the use of the controller property of the DDO (Directive Definition Object)
If you put in a the property like you did in your example, every instance of your directive will receive a brand
new MainViewCrtl. It seems you are not even using that in your code anyway.

As your directive has an isolate scope, the easiest way to put in the config var is like this:

app.directive('json', function($compile, $timeout) {
  console.log("json directive.");
  return {
    restrict: 'E',
    //controller: 'MainViewCtrl',
    scope: {
      child: '=',
      type: '=',
      config: '='
    }, //rest of directive unchanged, and unchecked by me!

and in your HTML you do:

<json child="jsonData" type="'object'" config='config'></json>

Does this help?
Regards
Sander

Sander Elias

unread,
Jul 24, 2014, 2:48:43 AM7/24/14
to ang...@googlegroups.com
The plunk did save now: http://plnkr.co/edit/IaudPZ?p=preview

Jonathan Matthew Beck

unread,
Jul 24, 2014, 8:54:02 AM7/24/14
to ang...@googlegroups.com
Sander,

Thanks!  That works in my project.  I am trusting that there is still only 1 `config`?  Or is each node creating a watch to the `config` above it?


On Thu, Jul 24, 2014 at 2:48 AM, Sander Elias <sande...@gmail.com> wrote:
The plunk did save now: http://plnkr.co/edit/IaudPZ?p=preview

--
Reply all
Reply to author
Forward
0 new messages