我正在使用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/