Search code examples
javascriptangularjsangularjs-ng-repeatangularjs-orderby

ng-repeat repeating item on splice instead of showing last item


I have the following ng-repeat that work fine.

<div ng-repeat="data in workflow.flow | orderBy:'+step_number'" ng-init="$stepIndex = workflow.flow.indexOf(data)">
    {{ workflow.flow[$stepIndex].step_number }}
</div>

$scope.workflow.flow

[
  {
    "id":"1334f68db820f664",
    "step_number":1,
    "tasks":[ { "id":"1334f68e3f20f665" } ]
  },
  {
    "id":"134967a5ba205f5b",
    "step_number":2,
    "tasks":[ { "id":"134972c5b420e027" } ]
  },
  {
    "id":"1334f68e7d209ae6",
    "step_number":3,
    "tasks":[ { "id":"1334f68ef6209ae7" } ]
  }
]

This is how it displays in my html:

1 
2
3

I have the below function that adds a step into the middle of an array:

$scope.insertStep = function() {

    var insertStepIndex = 1, 
        task_data = {"id": null, "step_number": (insertStepIndex+2), "tasks": []};

    //go through each item in the array
    $.each($scope.workflow.flow, function(index, step){
        //if the step number is greater then the place you want to insert it into, increase the step numbers
        if(step.step_number > $scope.workflow.flow[insertStepIndex].step_number) step.step_number++;
    });

    $scope.workflow.flow.splice((insertStepIndex+1), 0, task_data);

}

For some reason, the array with id 1334f68e7d209ae6 get's changed to step_number: 4 but get's completely ignored in the ng-repeat

This is what is displayed:

1
2
3
3

And when I console.log $scope.workflow.flow this is what I see:

[
  {
    "id":"1334f68db820f664",
    "step_number":1,
    "tasks":[ { "id":"1334f68e3f20f665" } ]
  },
  {
    "id":"134967a5ba205f5b",
    "step_number":2,
    "tasks":[ { "id":"134972c5b420e027" } ]
  },
  {
    "id":null,
    "step_number":3,
    "tasks":[]
  },
  {
    "id":"1334f68e7d209ae6",
    "step_number":4,
    "tasks":[ { "id":"1334f68ef6209ae7" } ]
  }
]

Does anyone know why this would happen?


Below is a code snippet:

angular.module('app', [])
  .controller('ctrl', function($scope) {
    $scope.workflow = {
      flow: [
      {
        "id":"1334f68db820f664",
        "step_number":1,
        "tasks":[ { "id":"1334f68e3f20f665" } ]
      },
      {
        "id":"134967a5ba205f5b",
        "step_number":2,
        "tasks":[ { "id":"134972c5b420e027" } ]
      },
      {
        "id":"1334f68e7d209ae6",
        "step_number":3,
        "tasks":[ { "id":"1334f68ef6209ae7" } ]
      }
    ]
    };

    $scope.insertStep = function() {

      var insertStepIndex = 1, 
          task_data = {"id": null, "step_number": (insertStepIndex+2), "tasks": []};

      //go through each item in the array
      $.each($scope.workflow.flow, function(index, step){
          //if the step number is greater then the place you want to insert it into, increase the step numbers
          if(step.step_number > $scope.workflow.flow[insertStepIndex].step_number) step.step_number++;
      });

      $scope.workflow.flow.splice((insertStepIndex+1), 0, task_data);
      
      console.log($scope.workflow.flow);
  }


  });
  

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>      
<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.14/angular.js"></script>
    <div ng-app="app" ng-controller="ctrl">

      <div ng-click="insertStep()">Insert Step</div><br /><br />

      <div ng-repeat="data in workflow.flow | orderBy:'+step_number'" ng-init="$stepIndex = workflow.flow.indexOf(data)">
        {{ workflow.flow[$stepIndex].step_number }}
    </div>
    </div>


Solution

  • It was because ng-init was only running once, not again when the array was updated. I have to use this instead:

    $scope.getIndex = function(data) {
      return $scope.workflow.flow.indexOf(data);
    };
    

    Like so:

    {{ workflow.flow[getIndex(data)].step_number }}