2017-10-07 89 views
0

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

这里是我的控制器代码:

.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,但在错误部分没有错误。有人请启发我究竟是什么问题可能..任何其他建议,使实时检索功能。提前致谢 。

+1

我认为你必须添加$范围。$适用()。 –

+0

嗨!非常感谢您的答复。请您告诉我我到底要把它放在哪里? – Amy

+0

你的代码看起来不错,也许你总是得到相同的数据 –

回答

2

要获得每5000毫秒的数据,你应该使用

$间隔服务$interval

0

首先,我认为你需要使用$间隔重复调用的。 其次,你必须在纯函数不能指定范围内的值的角度范围,想

var retrieveItems = function() { ... } 

尝试使用,

$scope.retrieveItems = function() {...} 

I think this wont be asking to $scope.apply()