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

时间:2016-05-10 22:01:36

标签: javascript angularjs

我在客户端有一个带有角度js和html的弹簧应用程序,我想每隔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>

任何帮助都将非常受欢迎

1 个答案:

答案 0 :(得分:3)

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

这是因为你使用setInterval修改$ scope.tab与$ scope.aat。

使用任何非Angular代码修改您的数据会使您的视图失去同步,因为Angular不知道更改(它没有完成工作,因此它不知道)。

您可以使用Angular的$interval代替setInterval来重复更新。或者在$scope.apply()中添加$scope.aat,告诉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);