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.

Answers


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

$scope.messages.push(obj)

Need Your Help

AngularJS $watch window resize inside directive

angularjs resize window directive watch

I have revealing module pattern which looks like this:

Preventing user to enter more then 3 numbers before decimal point in javascript

javascript php jquery html decimal

In PHP textfield: Prevent the user from entering more then 3 numbers before decimal point