G
G
godmodeon082017-10-03 01:39:30
Angular
godmodeon08, 2017-10-03 01:39:30

What is the correct way to update an array element?

Hey!
I'll start at the end of the question. You only need to update those elements of the array that have changed. You can not update the entire array!
[{"id":87510,"name":"1","date":1506983543"}{"id":87512,"name":"2","date":1506983543"}]
Controller:

$scope.getData = function() {
    $http.get('/api/arr/', {
      cache: true
    }).then(successCallback, errorCallback);
  };

  function successCallback(response) {
    if ($scope.arr== null) {
        $scope.arr= [];
      angular.merge($scope.arr, response.data);
    }
// но что делать, если нужно обновиться?
//пытался делать так:

  for (var i = 0; i < $scope.arr.length; i++) {
if ($scope.arr[i].name!=response.data[i].name) {
          $scope.arr[i].name= response.data[i].name
       }


}

get request I do every second by timer, but despite this, the condition $scope.arr[i].name!=response.data[i].name is never met because for some reason the data is the same, as if I have the same link. Although merge should work differently.
How to update a single item of an array without refreshing the entire array, with a get request?
The third day already I fight, it is impossible. An example would be very helpful. Thank you!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
godmodeon08, 2017-10-09
@godmodeon08

Decided by myself, it was necessary to use angular.copy.

N
Nicholas, 2017-10-03
@healqq

If you then shove it into ng-repeat, then you should put some custom trackBy.
Well, in general 1. angular.merge deprecated, 2. calling it to merge arrays - some kind of tin

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question