【问题标题】:AngularJS track by not removing $$hashKey, causing duplicate manipulationAngularJS 通过不删除 $$hashKey 来跟踪,导致重复操作
【发布时间】:2016-06-13 14:14:49
【问题描述】:

我目前正在通过 ng-repeat 向 DOM 添加“行”。 (AngularJS 1.5)

我通过我的$scope 中的一个数组有一个简单的 ng-repeat。我已经这样做了很多次,我从来没有遇到过问题。

这是我的 ng 重复: <li ng-repeat="row in panelRows track by $index" x-sb-panel-row row-data="row" class="row"></li>

在我的界面中,我添加了 2 个相同的对象,当然,通过添加 track-by,我希望它们被单独处理。

这是我在控制台中打印的对象,来自我的指令 x-sb-panel-row,通过 row-data="row" 传递(使用隔离范围和通过属性 row-data 的 2 路绑定)。

第 1 行:Object {name: "Test carousel (not really a carousel)", subText: "3 x 33%", columns: Array[3], $$hashKey: "object:68"}

第 2 行:Object {name: "Test carousel (not really a carousel)", subText: "3 x 33%", columns: Array[3], $$hashKey: "object:68"}

我的问题是$$hashKey: "object:68",显然使用track-by 应该删除它,但它没有,它为我的对象提供了相同的值。我该如何解决这个问题?

【问题讨论】:

    标签: angularjs angularjs-directive angularjs-ng-repeat


    【解决方案1】:

    我发现了我的问题。原来我是pushing 我的$scope.panelRows 的一个新行,但我推送的对象是一个已经被angularjs 解析的对象,有它自己的$$hashKey。

    因此,angularjs 认为他多次推动同一个对象。

    之前: $scope.panelRows.push(col)

    之后:

    $scope.panelRows.push(angular.copy(col))

    【讨论】:

    • 感谢您的回答,苦苦寻找解决方案。请注意,这也适用于使用 unshift()
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-10
    • 1970-01-01
    • 2017-04-01
    • 2016-02-27
    • 1970-01-01
    相关资源
    最近更新 更多