Hamed
Hamed

Reputation: 410

How to add element in the end of array Angular JS?

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.

Upvotes: 2

Views: 4530

Answers (1)

alwe
alwe

Reputation: 1497

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

$scope.messages.push(obj)

Upvotes: 2

Related Questions