【问题标题】:ng-repeat repeating item on splice instead of showing last itemng-repeat 在拼接上重复项目而不是显示最后一个项目
【发布时间】:2016-12-23 09:54:02
【问题描述】:

我有以下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 的数组 get 更改为 step_number: 4,但在 ng-repeat 中完全忽略了 get

这是显示的内容:

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

有人知道为什么会这样吗?


下面是代码sn-p:

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>

【问题讨论】:

  • 你能在这里创建一个sn-p
  • @iceman 我已将其添加到问题中

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


【解决方案1】:

你为什么不这样做:

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

您不必自己计算索引。我稍微改变了你的sn-p。不再使用ng-init,并使用data.step_number 而不是workflow.flow[getIndex(data)].step_number

您也不必使用 splice,您可以只使用 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>

【讨论】:

    【解决方案2】:

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

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

    像这样:

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

    【讨论】:

    • 我试图让你的生活更简单,请参阅下面的答案。
    猜你喜欢
    • 2018-02-12
    • 1970-01-01
    • 1970-01-01
    • 2016-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多