2

I have one array in Angular JS: $scope.messages = [];

In template I do ng-repeat:

<div ng-repeat="item in messages"></div>

When I try to do add a new element to the end of array like as:

angular.forEach($scope.messages, function (value, key) {
      $scope._lastMsg = key; // Get key of last element of array
});

$scope.messages[++$scope._lastMsg] = obj; // Do increment of next key and add new element obj

This way adds element not at end of array, always differently.

2

1 Answer 1

2

Try to push it at the end with Array.push()

$scope.messages.push(obj)
Sign up to request clarification or add additional context in comments.

4 Comments

I get error: TypeError: $scope.messages.push is not a function
It seems that $scope.messages was not initialized as an array before. Try first: $scope.messages = []
Was initialized in top of controller $scope.messages = []
@Hamed I doubt it. Post your complete controller code in the question.

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.