setInterval函数不会更改视图中的$scope

setInterval function does not change the $scope in the view

本文关键字:scope 视图 函数 setInterval      更新时间:2023-09-26

我在客户端有一个带有angular js和html的spring应用程序,我想每6秒更新一次视图中的信息,因为我调用了setInterval函数,但它不会更改视图这是我的代码

$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);

这是我的html视图

<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>

任何帮助都将不胜感激

相关:angular dos';t将更改应用于数组(删除套接字事件上的项目)

这是因为您正在使用setInterval用$scope.aat.修改$scope.tab1

使用任何非Angular代码来修改数据都会使视图不同步,因为Angular不知道更改(它没有完成工作,所以ofc也不知道)。

您可以通过使用Angular的$interval而不是setInterval来重复更新来修复此问题。或者在$scope.aat中添加$scope.apply(),告诉Angular重新检查所有内容是否有更改。

在这种情况下最好使用$interval$scope.apply()应该是最后的手段

$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);