Angular array with objects with index

I have a question about Angular watches in an array of objects.

I have an array of $ scope.chartSeries with objects as follows:

[{"location": {values}, "id":"serie-1", "meter":{values}, "name": "seriename", "data":[{1,2,4,5,7,4,6}]}]

This is used to draw a line chart using high speed charts.

I want to look at this array, and if the value changes, I want to know the index and the value that is changing.

I found several options for viewing, but none of them seemed to fit my situation. Can you help me?

+4
source share
2 answers

If you create and modify your array in ng-repeat, you can use the directive ng-changeand pass a parameter to it $index.

For instance:

<div ng-repeat="item in array">
   <input type="text" ng-model="item.location" ng-change="changeValue($index)"/>
</div>

$watch newValue, oldValue

$scope.$watch('array', function (newValue, oldValue) {
   for(var i = 0; i < newValue.length; i++) {
      if(newValue[i].location != oldValue[i].location)
        var indexOfChangedItem = i;
        //bla-bla-bla
   }
}, true);
+7

$watchGroup (watchExpressions, listenener).

,

$scope.chartSeries = [{"location": {values}, "id":"serie-1", "meter":{values}, "name": "seriename", "data":[{1,2,4,5,7,4,6}]}];

$scope.$watchCollection('chartSeries ', randomFunction(var) {
//code
});

, , .

+1

Source: https://habr.com/ru/post/1540348/


All Articles