0

Currently I am working on a project using AngularJS and I need to create a directive which is able to display (and alter) a "recursive structure" - something similar to a family. The structure defined in the controller looks like this:

$scope.members = [
            { firstName: 'Andrei', lastName: 'Croitoriu', age: 32 },
            { firstName: 'John', lastName: 'Doe', age: 25, members: [
                {
                  firstName: 'Jane', lastName: 'Doe', age: 24
                }
              ]}
           ];

I've managed to implement the directive which display the structure, but now I am struggling to implement a "Add Member" feature. Basically for each member I want to have a button next to it and be able to add a new member to the node. I would like to have a single method defined in the controller - all my attempts so far failed so in the plnkr code attached I've removed all attempts (left just a simple addMember method which just adds a new member to the top level collection.

Can anybody suggest any idea on how to pass the addMember method to the directives and implement the behavior I expect?

My code can be found here: http://plnkr.co/edit/hRzQiW?p=info

Thank you in advance!

Andrei

4
  • you want to add a member to the member's family members? Commented Jan 6, 2015 at 15:22
  • yes, I want when I click on add member button for a member to add a new member to it's members property. Hope that makes sense... Commented Jan 6, 2015 at 15:41
  • ok my answer is wrong then. i deleted the plunker link, but basic idea is add the addMember function in isolated scope Commented Jan 6, 2015 at 15:44
  • i have updated my answer with plunker link Commented Jan 6, 2015 at 16:34

1 Answer 1

2

fast tuning is to put addMember in familyMember directive, as the directive scope is isolated, the function under controller's scope is not accessible.

see the plunker here: http://plnkr.co/edit/h6vZu7?p=preview

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the suggestion @elaijuh! Seems to do the job for what I need!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.