使用 Angular UI-Scroll 在列表顶部添加项目

Adding item at top of a list using Angular UI-Scroll

我正在使用 Angular UI scroll. The example I am following is this one . It has a demo page here。它具有在特定位置添加列表项的功能。以下是代码摘录:

        $scope.addToList1 = ->
            $scope.firstListAdapter.applyUpdates (item, scope) ->
                newItem = undefined
                if scope.$index == 2
                    newItem =
                        id: idList1
                        content: 'a new one #' + idList1
                    idList1++
                    return [
                        item
                        newItem
                    ]
                return

此函数会将列表项添加到第 3 位。但是,我无法使用这个在顶部添加元素(即列表的顶部)。我试着用 scope.$index == 0 代替 scope.$index == 2。如果我使用 scope.$index == 1,它会在第二个位置添加元素。 ui-scroll 中还有一个前置函数,但我不确定如何使用它来始终在列表顶部添加项目。新添加的项目应该总是在位置 1.

如有任何建议,我们将不胜感激。

您可以使用 $index == -1

在列表顶部添加项目
$scope.addToList1 = ->
    $scope.firstListAdapter.applyUpdates (item, scope) ->
        newItem = undefined
        if scope.$index == -1
            newItem =
                id: idList1
                content: 'a new one #' + idList1
            idList1++
            return [
                item
                newItem
            ]
        return