2

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 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.