javascript - 数组拼接时如何调整索引

标签 javascript angularjs

我有一个照片管理器,当我删除不是最后一个索引的索引时,它会中断 View

型号:

var array = [{id:1, img:"index0"},{id:2, img:"index1"},{id:3, img:"index2"}]

如果我这样做..

array.splice(0);

索引 1 和 2 将被隐藏,所以当我调查时,我了解到索引 1 和 2 不会调整为索引 0 和 1,这可能就是它在 View 中中断的原因,因为我使用的是 ng-重复一遍,我该如何解决这个问题。

  <div class="col" ng-repeat="pic in pics track by $index">
    <img ng-click="delete()" ng-src="https://s3-ap-southeast-1.amazonaws.com/sample/posts/{{pic.id}}/{{pic.img}}.jpg"></img>
  </div>

Controller :

//HTTP REQUEST --> for brevity i didn't include the service
    $scope.delete = function(){
    PostService.DeletePic()
      .success(function (data) {
     $scope.pics.splice(index);
              }).
            error(function(error,status) {

          }) 
    }

最佳答案

调用array.splice(0);不会从数组中删除任何元素。

Syntax: array.splice(start, deleteCount[, item1[, item2[, ...]]])

deleteCount An integer indicating the number of old array elements to remove. If deleteCount is 0, no elements are removed. In this case, you should specify at least one new element. If deleteCount is greater than the number of elements left in the array starting at start, then all of the elements through the end of the array will be deleted.

(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice)

您需要调用array.splice(0, 1);来删除第一个元素。这应该可以解决您的“索引未调整”问题。

关于javascript - 数组拼接时如何调整索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31762442/

相关文章:

javascript - 从 css 调用图像到 javascript 并添加整数值

javascript - AngularJS css 动画 + 完成回调

javascript - 防止node-oracledb中的SQL注入(inject)

javascript - 使用 ajax setTimeout 将信息显示到 div 中

javascript - 谁了解 Windows Phone 上的设备方向?

python - 如何忽略发送到 Django REST Framework 的 CSRF token ?

angularjs - 我遇到了 phantomjs 和 karma 问题

javascript - $http 调用 Web API 2 不传递参数

javascript - 使用 Angular JS 和 Cordova 获取用户 60 秒的位置

javascript - 如何在 .map 中使用多个等待?