简体   繁体   中英

How angularJS update $scope data

I have written a code like this:

$http.post('/getUserInfo', Obj)

            .then(function(response) {
                //console.log(response.data);
                $scope.items = response.data;
            });

The data received in the response is set to $scope.items . If i run this code once again, will it change the whole $scope.items or it will only watch the change between the new and the old values of $scope.items ?

Assume that response.data contains huge amount of data.

Assuming you are concerned because you're iterating over the data with ng-repeat , what you need to do is use track by in the ng-repeat parameter to specify some condition that will tell angular that a particular element is unchanged. Then it will match that element up with the element from the old array and avoid reconstructing the html for the element.

<div ng-repeat="item in items track by item.uniqueid">
   ... content here ...
</div>

and make sure that the uniqueid attribute remains the same when the data remains the same but changes if the content of an element is updated. You might for example combine an id for the user with the date and time of the last update to provide a suitably unique value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM