4

I have a recursive array in js:

[{
  type: 'cond',
  conditionId: 1,
  conditions: undefined
},
{
  type: 'cond',
  conditionId: 2,
  conditions: undefined
},
{
  type: 'group',
  conditionId: undefined,
  conditions: [{
      type: 'cond',
      conditionId: 3,
      conditions: undefined
    },
    {
      type: 'cond',
      conditionId: 4,
      conditions: undefined
    }]
}]

It is displayed in AngularJS using ng-repeat recursively with inline template:

<script type="text/ng-template" id="recursive_template.html">
<div> some logic </div>
        <div ng-repeat="cond in cond.conditions" ng-include="'recursive_template.html'"></div>
  <div> some more logic  </div>
</script>

<div ng-controller="EditRulesController">
  <div ng-repeat="cond in conditions" ng-include="'recursive_template.html'" class="row">
  </div>
</div>

Everything works fine. Now I want to delete one item from inner ng-repeat. I can get the index of the item that I click on(to delete) with $index, also the object cond as parameter on ng-click. I can also access $parent but it points to the root of the object...

How to access the inner array without manually search through the whole conditions array recursively? Its strange that you can easily access the index of the inner array but not the actual array instance.

And any one advice on this issue? Thank you..

EDIT 21/03/2016

Before the accepted answer, the 'brute-force' version is implemented like this:

        $scope.deleteConditionClick = function (id) {
            deleteCondition($scope.conditions, id);
        };

        function deleteCondition(conditions, id) {

            var conds = conditions;
            if (conds) {
                for (var i = 0; i < conds.length; i++) {
                    if (conds[i].type === 'cond') {
                        if (conds[i].conditionId === id) {
                            conds.splice(i, 1);
                            return true;
                        }
                    } else if (conds[i].type === 'group') {

                        deleteCondition(conds[i].conditions, id);
                    }
                }
            }
        }

I hope it will help others with similar problem.

4
  • Are you not able to access it as cond.conditions.type, cond.conditions.conditionId ? Commented Mar 17, 2016 at 14:53
  • $parent.$parent - ngInclude is creating another scope between your parent repeat and your child repeat. Commented Mar 17, 2016 at 14:56
  • $parent.$parent gave me the controller scope, so the root of the array again. Commented Mar 17, 2016 at 15:48
  • @CoderJohn : I can access it, but I want to delete it and I need the containing array to delete from. The example is just two levels deep, imagine if there are 10 levels nested. Commented Mar 17, 2016 at 15:52

3 Answers 3

2

Have you tried setting you delete action to be ng-click="cond=undefined"? This would empty the deleted condition. You could then use ng-show/ng-if to control visibility in the view.

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

Comments

1

This is how I grab items with multiple indexes:

<div ng-repeat="x in outerArray" ng-init="outerIndex = $index">
  <div ng-repeat="y in innerArray">
    {{ innerArray[outerIndex] }}
  </div>
</div>

I think this is what you were asking? I hope it helps.

3 Comments

Thank you, I like this. The problem is that my object can be nested any level, not just 2. In that case a list of outer indexes is needed, which is again hard to manage.
Did @michaelbinks' solution fix the issue or should I propose something else?
Yes, it is a very good workaround, but I'm curious if you have any other ideas :)
0

You can pass the parent array and the object as parameters (eg.: ng-click="delete(conditions, cond)") and use .indexOf() function to find the index of the object (eg. $scope.delete = function(conditions, cond){index = conditions.indexOf(cond)...})

1 Comment

Yes, I tried this, but it gives me the root of the array because there is a $scope.conditions in controller. I also tried to rename that variable to have different name from inner arrays. Unfortunately it doesn't work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.