ng-repeat 拼接显示双

ng-repeat splice showing double

出于某种原因,当我将一个对象拼接到我的 ng-repeat 数组中时,它会加倍我拼接的内容并隐藏数组中的最后一个对象。

但是,如果我单击切换隐藏和 "refresh" ng-repeat,它会显示正确的数据。

有谁知道为什么会发生这种情况以及我可以做些什么来解决它?

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);

    }

    $scope.toggleHide = function() {
      $scope.hide = !$scope.hide;
    }


  });
<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-click="toggleHide()">Toggle Repeat</div>
  <br />
  <br />

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

我明白了。这是一个棘手但简单的部分。 ng-init 指令只执行一次。因此,当您将新数据推送到 array/list 时,您对 $stepIndex = workflow.flow.indexOf(data) 的分配不会更新。

因此添加范围函数将解决此问题,因为 Angular 将在函数返回值更改时自动调用该函数。

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
      var task_data = {
        "id": null,
        "step_number": (insertStepIndex + 2),
        "tasks": []
      };

      //go through each item in the array
      angular.forEach($scope.workflow.flow, function(step, index) {
        //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);

    }

    // This is a new function which I added to fix the problem
    $scope.getIndex = function(data) {
      return $scope.workflow.flow.indexOf(data);
    };

    $scope.toggleHide = function() {
      $scope.hide = !$scope.hide;
    };

  });
<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-click="toggleHide()">Toggle Repeat</div>
  <br />
  <br />

  <div ng-if="!hide">
    <div ng-repeat="data in workflow.flow | orderBy:'+step_number'">
      {{ workflow.flow[getIndex(data)].step_number }}
    </div>
  </div>
</div>