在拼接上重复重复项目,而不是显示最后一项

时间:2016-08-16 21:29:47

标签: javascript angularjs angularjs-ng-repeat angularjs-orderby

我有以下ng-repeat可以正常使用。

<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" } ]
  }
]

这是我在html中的显示方式:

1 
2
3

我有以下功能,在数组的中间添加一个步骤:

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

}

出于某种原因,ID为 1334f68e7d209ae6 的数组已更改为step_number: 4,但在ng-repeat

中完全忽略了

这是显示的内容:

1
2
3
3

当我在console.log $scope.workflow.flow时,这就是我所看到的:

[
  {
    "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" } ]
  }
]

有谁知道为什么会这样?

以下是代码段:

&#13;
&#13;
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);
  }


  });
&#13;
  

<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>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

这是因为ng-init仅运行一次,而不是在更新阵列时再次运行。我必须改用它:

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

像这样:

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

答案 1 :(得分:0)

你为什么不这样做:

<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'">
    {{ data.step_number }}
  </div>
</div>

您不必自己计算索引。我改变了你的片段。不再需要ng-init,而是使用data.step_number代替workflow.flow[getIndex(data)].step_number

你也不必使用拼接,你可以只使用push,因为你已经在ng-repeat中按step_number排序(除非你真的需要在数组中正确的对象)

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 than 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++;
      });

      // you don't have to use splice at all, since you're ordering by step_number in ng-repeat, you could just push it (unless you really need the objects in the right place inside the array):

      $scope.workflow.flow.push(task_data);

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


  });
  

<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'">
        {{ data.step_number }}
      </div>
    </div>