【问题标题】:$Timeout service for data refreshing not working(Angularjs)用于数据刷新的 $Timeout 服务不起作用(Angularjs)
【发布时间】:2017-10-07 05:25:05
【问题描述】:

所以我一直在使用 angularjs 和 symfony 开发一个应用程序。我试图每 5000 毫秒重复检索一次数据,所以我使用“$timeout”功能从服务器获取实时数据检索。

这是我的控制器代码:

.controller('clientController', [ '$rootScope','$scope', 'Api' , 'toastr',   
'$window', '$routeParams', '$filter'  , '$timeout',
 function($rootScope,$scope, Api  ,toastr, $window, $routeParams, $filter , 
 $timeout) {

     $rootScope.myVar=true;


 $scope.clients = [];
 var  retrieveItems = function () {

 Api.getAllClients()
  .then(function (result) {
    console.log('result', result);
    $scope.clients = result.data;
    $timeout(retrieveItems, 5000);
  }, function (error) {
    console.log('error', error);
  });
  };



 ...

}])

这是 vue 代码:

 <table class="table table-striped table-bordered" id="editable-datatable">
                           <thead>
                                <tr>
                                    <th>Username</th>
                                    <th>Email</th>
                                     <th>tel</th>
                                     <th>Actions</th>

                                </tr>
                            </thead>
                            <tbody>
                                <tr ng-repeat="client in clients" id="1" class="gradeX">
                                    <td>{{client.username}}</td>
                                    <td>{{client.email}}</td>
                                   <td>{{client.tel}}</td>

                                     <td  collapse=3>

                                                 <a style="border: 1px solid rgba(10, 6, 6, 0.25); padding: 4px;" ng-href="#!/client/{{client.id}}" ng-click="ClientDetailsController()">
                                                   <i class="icon-eye-open"></i> Details 
         </a> &nbsp;&nbsp;



<span style="margin-left: 10px"><a  data-toggle="modal" href  data 
target="#exampleModal" ng-click="setID(client.id)" >
                       <i class="icon-remove-sign"></i> Delete
                                                  </a></span>

                                        </td>


                                </tr>


                            </tbody>

                        </table>

我的问题是,每当我删除“retrieveItems”功能时,一切正常。我得到了所有的客户端,但是什么时候添加超时服务,这样我就可以每 5 秒刷新一次注册的客户端......客户端不会在 vue 上加载,但在错误部分没有错误。有人请告诉我问题到底是什么......任何其他建议可以使实时检索功能。提前致谢 。

【问题讨论】:

  • 我认为你必须添加 $scope.$apply().
  • 嗨!非常感谢您的回复。您能告诉我我应该把它放在哪里吗?
  • 你的代码看起来不错,也许你总是得到相同的数据

标签: javascript angularjs


【解决方案1】:

要每 5000 毫秒获取一次数据,您应该使用

$间隔服务$interval

【讨论】:

    【解决方案2】:

    首先,我认为您需要使用 $interval 进行重复调用。 其次,您必须在角度范围内分配范围值,而不是像

    这样的纯函数
     var  retrieveItems = function () { ... }
    

    尝试使用,

     $scope.retrieveItems = function () {...}
    
    I think this wont be asking to $scope.apply()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-15
      • 2016-06-09
      • 2015-06-04
      • 2023-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-24
      相关资源
      最近更新 更多