angularjs - 使用 Angular UI 滚动在列表顶部添加项目

标签 angularjs virtualscroll ui-scroll

我正在使用Angular UI scroll 。我下面的例子是this one 。它有一个演示页面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

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

任何建议都将受到高度赞赏。

最佳答案

您可以使用$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

关于angularjs - 使用 Angular UI 滚动在列表顶部添加项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35128972/

相关文章:

javascript - 通过 ids 数组过滤 Angular 数据

javascript - 我可以将函数导入 typescript 类文件吗?

angularjs - 将函数和数组作为 AngularJS Controller 传递有什么区别?

javascript - 过滤 Angular-ui 滚动

AngularJS ui-scroll 不工作(无法设置未定义的属性 'overflowY')

javascript - Angular 注销不会清除本地存储

Angular 7 cdk-virtual-scroll-viewport - 虚拟滚动

javascript - 使用 cdk-virtual-scroll (Angular 8) 滚动到底部

Angular cdk 滚动 : Updating Data Source not represented in view

angularjs - 如何将 "ui-scroll"与我自己的数据一起使用?