0

I have the following object named $scope.workflow:

{
  "name":"Demo #1",
  "flow":[
    {
      "tasks":[
        {
          "name":"Task 1 Step 1",
          "$$hashKey":"object:25"
        },
        {
          "name":"Task 2 Step 1",
          "$$hashKey":"object:93"
        }
      ],
      "$$hashKey":"object:21"
    },
    {
      "tasks":[
        {
          "name":"Task 1 Step 2",
          "$$hashKey":"object:29"
        }
      ],
      "$$hashKey":"object:22"
    }
  ]
}

And I have the following template

<ul>
    <li ng-repeat="data in workflow.flow" ng-init="$stepIndex = workflow.flow.indexOf(data)">

        <div class="step_container">
            <div class="step_header">{{ data.name }}</div>
            <div class="step_content">

                <div class="task_container" ng-repeat="task in workflow.flow[$stepIndex].tasks">
                    <div ng-click="editTask(data, task)">
                        <div class="header">{{task.name}}</div>
                    </div>
                </div>

            </div>
        </div>

    </li>
</ul>

When a task is clicked as per the template, editTask() Brings up the following:

$scope.editTask = function(step, task) {
    $scope.step_index = $scope.workflow.flow.indexOf(step_index);
    $scope.task_index = $scope.workflow.flow[$scope.step_index].tasks.indexOf(task);

    //code that opens up a popup model
}

The popup model as the following:

<div ng-click="deleteTask()">Delete me</div>

So along with my splice, I do a console.log to make sure it is selecting the correct object like so:

$scope.deleteTask = function() {
    console.log($scope.workflow.flow[$scope.step_index].tasks[$scope.task_index]);
    $scope.workflow.flow[$scope.step_index].tasks.splice($scope.task_index, 1);
}

The console.log confirms that I am selecting the right task but if I try to delete the last task it will delete the second to last on instead. And if I try to delete the last task after that, it won't delete any of them.

Does anyone know what I could be doing wrong? It makes no sense because the console.log is literally outputting what I want to delete every single time but the splice isn't working right.

bryan
  • 8,879
  • 18
  • 83
  • 166
  • What about using `$index` instead of calculating the index `ng-init="$stepIndex = workflow.flow.indexOf(data)"`? – lenilsondc Nov 28 '16 at 18:36
  • @LenilsondeCastro I could, but regardless, `step_index` and `task_index` are both correct when consoling the `workflow` object so its confusing me greatly. – bryan Nov 28 '16 at 18:40
  • Can you turn your code blocks above into a **runnable** [mcve] using Stack Snippets (the `[<>]` toolbar button)? That would make it easier to help you, and forestall answers/comments saying you're mistaken... – T.J. Crowder Nov 28 '16 at 18:44
  • maybe you need to eval if the task array is empty after the splice, and then, remove the flow entry also. – Joao Polo Nov 28 '16 at 18:48
  • It was just a tip for a good practice. It will turn your code into a much simpler piece (also for performance issues) and perhaps making it easier to solve this issue as well. :{D But I'm with @T.J.Crowder would be much better if you could reproduce the issue to us. – lenilsondc Nov 28 '16 at 18:51
  • @bryan assuming that this answer has been answered here http://stackoverflow.com/questions/40851917/keeping-track-of-nested-ng-repeat-indexs/40852071#comment68921382_40852071, you may dalete this one, or is it still necessary? – lenilsondc Nov 29 '16 at 00:21

0 Answers0