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

812 views Asked by At

I am using Angular UI scroll. The example I am following is this one . It has a demo page here. It has a function to add a list item at specific position. Following is the excerpt of the code:

        $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

This function will add list item at 3rd place. However, I am not able to use this one to add element at top ( i.e. to the top of the list ). I tried putting scope.$index == 0 instead of scope.$index == 2. If I use scope.$index == 1, it will add element in the second position. There is also a prepend function in ui-scroll, but I am not sure how to use it to add item always at the top of the list. The newly added item should always be a at the position 1.

Any suggestions will be highly appreciated.

1

There are 1 answers

0
shrestha_aj On BEST ANSWER

You can add items at the top of the list by using $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