【问题标题】:How can I replace an object in array that is displayed using ng-repeat?如何替换使用 ng-repeat 显示的数组中的对象?
【发布时间】:2015-07-08 22:36:45
【问题描述】:

我有一个使用 ng-repeat 显示在表格中的项目数组。当您单击一个项目时,该项目将从服务器中拉出,然后应该使用更新的项目更新表格。

单击表格中的项目时获取更新项目的功能:

$scope.getUpdatedItem = function(item){
    itemService.getItem(item).then(
        function(updatedItem){
            item = updatedItem;
        },
        function(error){
            //Handle error
        }
    );
};

我正在使用以下方式显示项目:

<tr ng-repeat="item in myItems">

问题:表中的项目永远不会更新。

在 ng-repeat 中更新项目的最佳方式是什么?我可以在 ng-repeat 中使用“track by $index”吗?还是我必须遍历 myItems 才能找到要替换的项目?

更新:

一个可能的解决方案是代替使用

item = updatedItem,

使用:

var index = $scope.myItems.indexOf(item);
$scope.myItems[index] = updateItem;

但是,我觉得应该有一种“更清洁”的方式来做到这一点。

【问题讨论】:

  • itemService 看起来像什么?
  • 在我看来,您的控制器中需要一个变量,您可以根据updatedItem 中的数据设置该变量。当item = updatedItem 运行时,item 的作用域就是那个函数。
  • 有一张还是两张桌子?一个是项目列表,一个是选定项目?
  • 只有一张桌子。
  • @jcuenod 你能举一个简短的例子来说明你的意思吗?

标签: javascript angularjs angularjs-ng-repeat


【解决方案1】:

如果我能正确理解您的问题

这样的东西能行吗?

<!-- template code -->
<table>
    ...
    <tr ng-repeat="(index, item) in items">
        <td>{{item.name}}</td>
        <td>
             {{item.detail}}
             <button ng-if="!item.detail" ng-click="loadItem(index)">
        </td>
    </tr>
</table>

// Controller Code
$scope.items = [...]
$scope.loadItem = function(index){
    itemService.getItemDetail($scope.items[index]).then(function(itemDetail){
        $scope.items[index].detail = itemDetail;
    });
};

【讨论】:

  • 由于ng-repeat 的范围,这可能不起作用。它只监视对象本身的变化,而不是它的属性。
  • 我认为当任何 $q 承诺被解决时,整个范围会再次递归检查
  • 我刚刚设置了一个 plunker。它似乎确实有效 plnkr.co/edit/1O0XFkz5oH4UKRQt0jV1 。 ng-repeat 没有深入的观察,因为它只需要知道什么时候有新元素,但我猜是模板中的 {{item.extra}}。
  • 是的,我刚刚创建了一个 jsFiddle 来检查它,它看起来很好(我应该先检查一下,抱歉)-jsfiddle.net/neridum/cvdz9r16 也许直接使用索引有助于避免这个问题。跨度>
  • 我在stackoverflow.com/questions/15646304/…这个话题上找到了一个很好的答案
【解决方案2】:

item 可能以引用列表中的项目开始,但当你说:

item = updatedItem;

您重新设置该绑定——您不再指的是列表中的项目,而是指在您的承诺中返回的断开连接的项目。您需要修改项目,如下所示:

function(updatedItem){
  item.varA = updatedItem.varA
  item.varB = updatedItem.varB
  ...
}

或者,如果它变得太长,你可以考虑一个看起来更像这样的项目数组:

var items = [ 
  { data: item1 },
  { data: item2 },
  { data: item3 }
};

此时您的更新函数将如下所示:

function(updatedItem){
    item.data = updatedItem;
},

【讨论】:

    【解决方案3】:

    没有更清洁的方法(然后是您的更新)。 正如您所注意到的,当您在回调函数中更改 item 时,您会更改本地引用,而不是数组中的原始项目。

    您可以通过使用ng-repeat 中的$index 来改进这一点,而不是自己计算:

    <div ng-click="getUpdatedItem(item, $index)"> </div>
    

    在你的控制器中:

    $scope.getUpdatedItem = function(item, index){
        itemService.getItem(item).then(
        function(updatedItem){
            $scope.myItems[index] = updateItem;
        },
        function(error){
            //Handle error
        }
        );
    };
    

    您也可以改用angular.copy,但效率要低得多:

    function(updatedItem){
        angular.copy(updateItem, item);
    },
    

    【讨论】:

    • 应避免使用 $index,因为它是 ng-repeat 索引,而不是绑定到视图的范围对象的实际数组索引;有时,如果您进行排序或过滤,这可能会出现严重错误,例如您有一个数组 bird[0] = parrot;鸟[1] = 乌鸦;如果您过滤 crow,您的网格将导致 $index = 0 但实际上,crow 位于索引 1。
    【解决方案4】:

    我刚刚在这个问题上花了几个小时。我无法使用来自@eladcon 的$index 解决方案,因为我的ng-repeat 也使用了过滤器,如果过滤了行/项目,则索引不正确。

    我以为我能做到这一点:

    $filter('filter')($scope.rows, {id: 1})[0] = newItem;
    

    但这不起作用。

    我最终迭代数组直到找到匹配项,然后使用迭代中的$index(而不是ng-repeat)将数组项设置为新项。

    // i'm looking to replace/update where id = 1
    angular.forEach($scope.rows, function(row, $index) {
      if (row.id === 1) {
        $scope.rows[$index] = newItem;
      }
    })
    

    请看这里: https://codepen.io/anon/pen/NpVwoq?editors=0011

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-03
      • 2019-08-13
      • 1970-01-01
      相关资源
      最近更新 更多