amani92
amani92

Reputation: 913

setInterval function does not change the $scope in the view

I have a spring app with angular js and html in the client side, and I want to update the infromation in the view every 6 seconds for that i call setInterval function but it doesn't change the view this is my code

$scope.aat=function(){
                $http.get("http://localhost:8080/projet/getInspectionEnCoursDinspection")
    .success(function (data) {

     $scope.tab1=data;
     console.log("size="+ $scope.inspectionEnCoursDinspection1.length);

   })   
} ,

setInterval(function(){ $scope.aat() },60000);

and this is my html view

<table class="table table-bordered">
<tr ng-repeat="insp in tab1">
<td>Mat{{insp.vehicule.matricule}}

<div ng-repeat="aa in insp.tests">
Test<ul><li ng-repeat="defaut in aa.defauts">{{defaut.nomDefaut}}</li></ul>
</div>

</td>

<td><button ng-click="annulerTest()"
                                    class="btn btn-default btnt">
                                    <i class="glyphicon glyphicon-remove"></i>
                                </button></td>
</tr>
</table>

any help will be most appreciated

Upvotes: 2

Views: 322

Answers (1)

vtange
vtange

Reputation: 649

Related: angular doesn't apply changes to array(remove item on socket event)

This is because you're using setInterval to modify $scope.tab1 with $scope.aat.

Using any non-Angular code to modify your data will de-sync your view because Angular doesn't know about the change (it didn't do the work, so ofc it wouldn't know).

You could fix this either by using Angular's $interval instead of setInterval to repeat your updates. Or add $scope.apply() in $scope.aat to tell Angular to re-check everything for changes.

It is better to use $interval in this case. $scope.apply() should be a last resort.

$scope.aat=function(){
                $http.get("http://localhost:8080/projet/getInspectionEnCoursDinspection")
    .success(function (data) {

     $scope.tab1=data;
     console.log("size="+ $scope.inspectionEnCoursDinspection1.length);
     //this--->$scope.apply();
   })   
} ,

//OR this-->$interval(function(){ $scope.aat() },60000);

Upvotes: 3

Related Questions